-1
public static void main(String[] args)  
{  
    // Let's print the classloader name of current class.   
    //Application/System classloader will load this class 
    Class b= int.class; 
    Class c=ClassLoaderExample.class; 
    ClassLoaderExample a= new ClassLoaderExample(); 
    System.out.println(c);
}  

what is .class in here and what it specifies? Are c and a different?

  • According to this, both a and c are objects of ClassLoaderExample class, then why a.getCalss and c.getClass has different outputs? – Trusha_Patel Mar 09 '21 at 06:15
  • No, it's same as above. But doesn't clear my confusion. I am trying to understand. Perhaps it's my low grasping power, but it's still unclear to me. – Trusha_Patel Mar 09 '21 at 06:18

1 Answers1

1

a class is a definition of type, an object is an instance of type. See here: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html

c is an object of type Class. So, c, as such, is "an object of certain type", but it's value conveys information about the type of some other entity.

Yes, it's confusing for beginners. Fortunately, one doesn't have to use Class type often.

62mkv
  • 1,444
  • 1
  • 16
  • 28
  • So a and c are both objects, but of different classes? – Trusha_Patel Mar 09 '21 at 06:17
  • that's exactly what you see in your code: `Class c = ...; ClassLoaderExample a = ...;` – 62mkv Mar 09 '21 at 06:19
  • Let me articulate. Here c is object of class Class. But it refers to class ClassloaderExample. Just like b is object of class Calss but refers to int class. So when I print b, it's int but b.getClass(), it's java.lang.Class. But a is object of class ClassLoaderExample, and a.getClass() will say same. – Trusha_Patel Mar 09 '21 at 06:23
  • Am I going correctly? – Trusha_Patel Mar 09 '21 at 06:24
  • `a.getClass` is a method that returns a value, that is same as `c`. I don't really understand, sorry, but what is the confusion here? – 62mkv Mar 09 '21 at 06:26
  • `b` and `c` both are objects of `Class` type (which is a parametrized type, by the way, but let's not get there yet). `a` is a "regular" object, of `ClassLoaderExample` type. – 62mkv Mar 09 '21 at 06:27
  • Oh yes! Got it. The previous question does answer my query. Thanks! Will close this – Trusha_Patel Mar 09 '21 at 06:33