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();
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:
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?
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:
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.
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