This question already has answers here:
Difference between MyClass.class and Class.forName("className")
(4 answers)
When does static initialization happen?
(3 answers)
How does Class.forName() work?
(4 answers)
What is a class literal in Java?
(10 answers)
Closed 3 years ago.
Class.forName() and Class.class both can get the Class object.What is the different of them?
I noticed that static method block don't execute when I got class object by Class.class.Is the class file not loaded by JVM yet?
Related
This question already has answers here:
Why can I create an variable with type of parent class
(2 answers)
What does it mean to "program to an interface"?
(33 answers)
Closed 12 months ago.
I am currently learning Java as a past time, and I am troubling in absorbing this instantiation syntax.
I know that if a Main class is created, I can create an object by using this
Main obj = new Main();
But in further lectures (specifically inheritance and polymorphism), they are creating objects as
Parentclass object = new Subclass();
I've tried using
Subclass object = new Subclass();
and have the same output.
Can you please tell me the difference about these two?
This question already has answers here:
Java: define terms initialization, declaration and assignment
(8 answers)
What happens in memory if we just declare a variable without initialization in java?
(3 answers)
What happens to a declared, uninitialized variable in Java?
(3 answers)
Java, What is the difference between assigning null to object and just declaration
(2 answers)
Closed 4 years ago.
Assuming we have a defined class named Body and we want to create an instance of that class, what is the difference between the two following declarations:
Body a1;
Body a1 = new Body();
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.
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:
Methods vs Constructors in Java
(11 answers)
Closed 7 years ago.
is setMnemonic(int) a constructor or method?
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
It is a method. A constructor will always consist of the name of the class being called on. This is a method, because it is called after an instance of some class, using the dot.