Java - Why is it necesary to use Class class's instance methods write classname.class.method or instance.getClass().method? -
why necesary use class class's methods write classname.class.method or instance.getclass().method?
for example:
public class someclass{ public static void main (string[] args){ someclass sm = new someclass(); //the correct ways: system.out.println(sm.getclass().getname()); //or system.out.println(someclass.class.getname()); } }
class class's instance methods need class (it can seen when in system.out.println(sm.getclass().getname();
because sm.getclass() returns class) why not correct write system.out.println(someclass.getname();
, necessary write "class" in middle if getname() method called class? because someclass class not considered instance of class class? why sm.getclass() considered instance of class class then?
thank you.
new someclass()
creates instance of someclass
class :
someclass someclass = new someclass();
it doesn't provide class
instance of someclass
getclass()
instance method of object
class :
class<? extends someclass> clazz = new someclass().getclass();
these 2 totally different things.
Comments
Post a Comment