0

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... 
2
  • 2
    console.write() would lead to nullpointer exception since it is not initilazed Commented Jan 7, 2014 at 8:55
  • There is nothing in this question about 'difference between Object and Class. Please fix your title. Commented Jan 7, 2014 at 9:06

8 Answers 8

4
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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok thanks for this explanation but when would it be useful to use the ConsoleWriter alone without initializing it in which cases?
@user3135757 It wouldn't be useful to never initialise it, but seperating out the declaration and initialisation allows it to be initialised in different ways (in the simplest case think of an if statement with two different constructors being used), or in a completely seperate function (which then returns a new ConsoleWriter())
It is just a pointer. There is nothing much you can do with it. Though you can use to get the return value of a function or as an argument in function. If you have some subclass then you can use it as polymorphic reference. But to summarize it is not useful unless it points to some actual object.
2

"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.

Comments

1

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.

Comments

1

console.write(); will give you runtime exception most probably null pointer exception

But

 ConsoleWtiter console1=new ConsoleWriter(); console1.write(); 

would give you

Console Writing... 

Comments

1

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.

Comments

1

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

Comments

1

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.

Comments

0

Any class without a constructor specified always has the default constructor you can use. In your case this is:

public class ConsoleWriter{ public ConsoleWriter(){ } } 

1 Comment

I fail to see how this answers the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.