Reputation: 165
lets say we have this class without any constructors and instances variables
public class ConsoleWriter{
public void write(){
System.out.println("Console Writing...");
}
}
and you have another class App with 2 variables console and console1
public class App{
ConsoleWriter console;
ConsoleWtiter console1=new ConsoleWriter();
}
Now I understand that console1 is an object of ConsoleWriter while console is just a reference type to ConsoleWriter.
When having such case (No constructors, No instance Variable)
what is the difference ?
or how would it be useful to create the Object console1
if we could just do it using the console
.
console.write();//would output Console Writing...
console1.write();//would output Console Writing...
Upvotes: 0
Views: 167
Reputation: 311055
Now I understand that console1 is an object of ConsoleWriter
No. console1
is a reference to ConsoleWriter
which isn't null, because you initialized it.
while console is just a reference type to ConsoleWriter.
Correct. console1
is a reference to ConsoleWriter
which is null, because you didn't initialize it.
what is the difference ?
The difference is that you didn't initialise one of them.
Upvotes: 1
Reputation: 122026
You really need to know the difference between Declaring, Instantiating and Initializing an Object
ConsoleWriter console;
ConsoleWiter console1=new ConsoleWriter();
No, the below lines are wrong
console.write();//would output Console Writing...
console1.write();//would output Console Writing...
You face a NullPointerException in first line.
You have'nt to initialize it and now it's holding default value null
.
Where as second line executes the write()
method. In order to use it further you have to initialize it.
Recomminding : Declaring, Instantiating and Initializing an Object
Upvotes: 1
Reputation: 36304
"Now I understand that console1 is an object of ConsoleWriter while console is just a reference type to ConsoleWriter." - Wrong. Both console and console1 are references.. The difference is that console1 points to a ConsoleWriter object whereas console doesnt point to anything.
So. console.anyFunction() will give you an exception (NulPointerException) because console is not initialized whereas console1.someFunction() will work as console1 points to a ConsoleWriter Object.
Upvotes: 2
Reputation: 69035
console1 is an object of ConsoleWriter
Incorrect it is a reference that will point to Object of type ConsoleWriter. Since uninitialized it will point to null
. Also you cannot call methods on this as it is uninitialized.
console.write();//would output Console Writing...
This is also incorrect. Must be initialized first.
ConsoleWtiter console1=new ConsoleWriter();
This is equivalent to
ConsoleWtiter console1;
which creates a reference which will point to Objects/instances of class ConsoleWriter. Since it is an instance variable it is assigned default value i.e null.
console1 = new ConsoleWriter();
At this point the reference console actually points to an Object of class ConsoleWriter. It is only after this(initialization) you can call methods of the class.
Upvotes: 4
Reputation: 20140
Any class
without a constructor specified always has the default constructor you can use. In your case this is:
public class ConsoleWriter{
public ConsoleWriter(){
}
}
Upvotes: 0
Reputation: 282006
console
isn't usable. It's not a ConsoleWriter
; it's null. If you try to call its methods, you'll get a NullPointerException. This isn't C++, where
ConsoleWriter console;
constructs a ConsoleWriter
.
Upvotes: 1
Reputation: 13854
console.write();
will give you runtime exception most probably null pointer exception
But
ConsoleWtiter console1=new ConsoleWriter();
console1.write();
would give you
Console Writing...
Upvotes: 1
Reputation: 96016
See the JLS:
For all reference types (§4.3), the default value is null.
So when you write:
ConsoleWriter console;
console1.write();
It's like writing:
null.write(); //Will throw NullPointerException
On the other hand, when you do:
ConsoleWriter console1=new ConsoleWriter();
Then you are constructing a new object of type ConsolWriter
.
Upvotes: 1