What exactly is MyClassName.class in Java [duplicate] - java

This question already has answers here:
What does .class mean in Java?
(8 answers)
Closed 8 years ago.
What exactly is the meaning of the following construction: MyClassName.class ?
At first I thought MyClassName.class would represent the access of a static class variable defined for MyClassName class, but it that'd be true then the following assignment should be possible:
MyClassName m = new MyClassName();
Class<MyClassName> clazz = m.class; //access static class variable by using an instance variable
What is the true meaning of MyClassName.class? Is it a static class variable? is it a Java special construction?

Every class in Java has an associated instance of java.lang.Class which contains metadata about the class i.e its attributes, types, methods, super class etc.
Here MyClassName.class is called class literal. Link to Java doc for further info - http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.8.2

Related

Why I have this error in java [duplicate]

This question already has answers here:
What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?
(11 answers)
Closed 4 years ago.
I am a new learner in java, and I cannot figure out this error. I've already created a class outside the Main class, why can't I creat a object in Fraction?
Fraction is defined as inner non-static class inside Main class. Hence to instantiate an object of Fraction you would first need to define an object of Main. And using that define an object of Fraction.
e.g.
Main m = new Main();
Fraction a = m.new Fraction(in.nextInt(), in.nextInt());
Or the other option you have is to define Fraction class as static.
e.g.
static class Fraction {
... and you class definition...
}
The right option depends on your usage entirely. But as per the sample you pasted option #2 defining the class as static would suit you more.

What is class static variable in java [duplicate]

This question already has answers here:
What is a class literal in Java?
(10 answers)
Closed 5 years ago.
Where is class static field is declared and how this field is associated with every classes?
System.out.pritnln(String.class);
System.out.pritnln(Student.class);
output:
class java.lang.String
class Student
It depends on the implementation of the VM, but if you do a heap dump of the Hotspot JVM you can see a special object for each class which holds the static fields.
You can access them dynamically at runtime by using reflection.

Can an object belong to two classes? [duplicate]

This question already has answers here:
Does the System.out object belong to class System or class PrintStream? [closed]
(7 answers)
Closed 6 years ago.
Just started learning Java, through the "hello world" app, k learned about the object System.out.
Out is an object, but to use it, we have to write system in front of it. It's obvious and my book says that out belongs to system class.
But later in the book, my book says out also belongs to PrintStream class. That is what enable us to use println methods because they both belong to PrintStream class.
I am confused what class does out belong to?
Also, is it just a convention that for objects like out, we have to write the class as well whenever we use it. For something like;
String greeting = "Hello, World!";
If we want to use .length() method which I guess also belongs to string class, we DONT write:
int n=String.greeting.length()
Instead, it's just:
int n=greeting.();
out is a (static) member variable of class System.
It is an instance of class PrintStream.
class Foo {
String x;
}
x is a string. It is a member of class Foo. Same idea.

Array-instance of class [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I found an instantiation wich I do not understand how works.
The instantiation looks like this:
public static Class instance[] = new Class[arraySize];
If my guessing is right, the instance is an array?
How will this work?
This declares an array of Class objects references. It is equivalent to the other syntax of [] after the type.
You would access it just like a normal array:
instance[0] = ...
instance[1] = ...
public static Class instance[] = new Class[arraySize];
public is the access modifier. This one means that this variable is visible in your entire project
static means that this variable is a "class" field it means that it belongs to the entire class and you can access it by ClassName.nameOfTheVariable or if you are accessing it from inside of class it is declared you could use just nameOfTheVariable.
Class in this context is a type and you should treat is as a type of object
[] means that this is an array you could also write Class[]
= is assignment operator
new is the word that annunciates that there will be memory allocation and constructor invokation after it
After new there is initialization of array of arraySize length.

What is a Type Literal in Java? [duplicate]

This question already has answers here:
Use of TypeLiteral in java
(5 answers)
Closed 9 years ago.
There is an ASTNode subclass called TypeLiteral.
The description of this type of code says that it looks like
( Type | void ) . class
I don't believe I have ever seen anything like this.
Can anyone provide an example of a TypeLiteral, and perhaps additionally of it being used in a Java program?
There is a class called Class that represents the class of an object. It contains various useful methods for introspection on that class and objects of that class. One way to get a class object is to use the Object method getClass(). But if you want to refer to a Class by name of its class, you use a class literal.
Some examples:
class Foo {
void method(Object b) {
if(b.getClass() == Foo.class) {
Foo f = Foo.class.cast(b);
}
Foo.class.getResourceAsStream(...);
Foo.class.getMethod("method", Object.class);
}
}

Categories