简体   繁体   中英

Difference between Object and Class

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

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

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 .

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

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

But

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

would give you

Console Writing...

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 .

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.

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

public class ConsoleWriter{
  public ConsoleWriter(){
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM