0

As i know Class c = new Class(); makes new instance of Class then we can use this Class methods and variables etc. then What can i do with Class c; declaration without =new Class(); assignment. also i saw examples like these Class c= b.something(); , Class c=a; which is, another class type is assigned to Class c. what things occur when we assign a Class to another Class.

  • `Class c` is just a reference, without an initialization you can't really do anything with it. – Nicholas K Mar 05 '19 at 11:33
  • 1
    Don't mistake classes with objects and/or instances – Lino Mar 05 '19 at 11:33
  • If you do not initialize a field `Class c`, it will be initialized with `null`. If you do not initialize a local variable `Class c`, Java will force you to initialize it before you can read its value. – Turing85 Mar 05 '19 at 11:34
  • 1
    This question lacks research and doesn't add value – IfOnly Mar 05 '19 at 11:36
  • 1
    Possible duplicate of [Difference between initializing a class and instantiating an object?](https://stackoverflow.com/questions/15074083/difference-between-initializing-a-class-and-instantiating-an-object) – fantaghirocco Mar 05 '19 at 11:42
  • what research...? – info catalyst Mar 06 '19 at 10:11

6 Answers6

3

Without assigning a reference to your variable (be it a newly created one, or be it some existing reference that say a method returns to you) you can do exactly nothing (productive, you can use such nulls of course to have the compiler throw error messages at you, or worse, run into exceptions at runtime).

Like any Whatever theThing; is just a declaration. It tells the compiler that there is a variable that should be used to hold references to the given type. But initially, that thing is simply null.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

Without an initialization variable Class c will be null.

Egor
  • 1,334
  • 8
  • 22
  • 4
    If it is a local variable, it will be uninitialized and every read prior to initialization will result in a compiletime error. – Turing85 Mar 05 '19 at 11:35
2

There are a few definitions.

Class c = new Class(); - initialization
Class c; - declaration

In case of declaration (from specification):

...its value will be undetermined until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object. For that, you need to use the new operator, as described in the next section. You must assign an object to the variable before you use it in your code. Otherwise, you will get a compiler error.

Ruslan
  • 6,090
  • 1
  • 21
  • 36
1

Class c; refers to an instance of a class.

Without assignment, it is set to null. With assignment is is set to the new instance.

By not assigning it, you can first declare it, and then assign it later.

Benefits

  • If initialization takes a long time, you can delay it.
  • It may depend on other resources.
  • The declaration order of multiple items does not need to be the same as their creation order.
  • You can create the instance on demand by using a factory method to get it; Then it is only created if and when it is used.
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78
  • 1
    If it is a local variable, it will be uninitialized and every read prior to initialization will result in a compiletime error. – Turing85 Mar 05 '19 at 11:36
  • Mr. Steven Spungin! your Answer is in Very easy to understand coz of Simple English, its short but deep ! Thanks – info catalyst Mar 06 '19 at 10:09
1

Say that you going to a coffee shop where you will get a coffee in a cup. The machine when switched on will pour the hot coffee into a cup. If cup is not placed properly, coffee will not be available the way you want, and if the machine is not started cup won't be having any coffee.

Coffee cup;  // is your cup to hold coffee

new Coffee();  // is when the machine gives you a coffee object

So Coffee cup = new Coffee(); means coffee object is created and is available in cup which is a reference.

Now Coffee cup; will be simply an empty cup (without any Coffee) which is defined as null and new Coffee(); is a prepared coffee which is not stored anywhere, it can be floating either on floor or desk or table.

Reference : Calling methods on reference variable vs Calling methods on a new object

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
1

There are three steps when creating an object from a class

Declaration − A variable declaration with a variable name with an object type this is when you just state to the program that there would will be an object type of animal name cat.

e.g: Animal cat;

Instantiation - this is when a memory is allocated to the object. it is done using the new operator. Initialization- the new operator is followed by the constructor of that object to store values on the allocated memory;

here is an example of instantiation and initialization on the same line.

e.g: Animal cat = new Animal(Kitty);

I hope this helps.