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.
Related
This question already has answers here:
Synthetic Class in Java
(13 answers)
Closed 7 years ago.
I was going through the java Method.class (Decompiled) and I found something that caught my eye.
#Override
public boolean isSynthetic() {
return super.isSynthetic();
}
"Synthetic". What does that mean?
Is it usable in code?
.
And since I found this in the Method class, I was wondering, is it that an entire method could be "synthetic", or is it that it contains something synthetic?
Thanks in advance.
.
not a copy of What is the meaning of “static synthetic”?
Any constructs introduced by the compiler that do not have a
corresponding construct in the source code must be marked as
synthetic, except for default constructors and the class
initialization method.
http://www.javaworld.com/article/2073578/java-s-synthetic-methods.html
This question already has answers here:
Java: NoSuchMethodException when method clearly exists
(4 answers)
Closed 7 years ago.
I am trying to add static methods from an existing class to a HashMap. All the methods are located in the same class with the following code :
map.put("x", myClass.class.getMethod("addX"));
map.put("y", myClass.class.getMethod("addY"));
When I run the code I get java.lang.NoSuchMethodException: package.myClass.addX.
Any ideas?
You should use getDeclaredMethod() method instead of getMethod().
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
This question already has answers here:
The difference between Classes, Objects, and Instances
(16 answers)
Closed 8 years ago.
There can only be one instance of class and can have many objects...? If not then can you please explain me difference between Instance and Object of the class
Instance and object of class are same things.
Theoratically Instance represents certain object at one time.
Example: Suppose you have Employee Class(Structure) with firstName and lastName
your instance or object(Value) represent Employee with firstname "Sanjay" and lastname "Rabari"
This question already has answers here:
why main method can't be of default scope? [duplicate]
(9 answers)
Why is the Java main method static?
(37 answers)
Closed 8 years ago.
Why is the main declared as public and static?
public static void main(String arg[])
{}
acording to ans in java
"The method is static because otherwise there would be ambiguity: which constructor should be called?"
public - The main method is called by the JVM to run the method which is outside the scope of the project therefore the access specifier has to be public to permit a call from anywhere outside the application.
static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.
void - Java is a platform independent language, therefore if it returns some value, then the value may have a different meaning between different platforms so unlike C it can not assume a behavior of returning value to the operating system.