Rajat Singh
Rajat Singh

Reputation: 155

What is the exact meaning of instantiate in Java

I am new to Java, and I came across the word instantiate in this sentence:

A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

What does this word mean? I know it's embarrassing and I should've researched more but I just cannot understand what it means.

Upvotes: 8

Views: 64485

Answers (7)

majid khan
majid khan

Reputation: 31

when you create an instance of a class or simply called as an object

for ex: Bike abc = new Bike();

as soon as you create this object using the new keyword, a new block of memory is created and object "abc" will now be pointing to that new block of memory, this is called as instantiation in java.

Upvotes: 3

Novaterata
Novaterata

Reputation: 4809

Instantiate in Java means to call a constructor of a Class which creates an an instance or object, of the type of that Class. Instantiation allocates the initial memory for the object and returns a reference. An instance is required by non-static methods as they may operate on the non-static fields created by the constructor.

Static methods don't need an instance and should not be stateful, i.e. should not rely on changing data. They are essentially free functions that are associated with the type and not a particular instance. When you want to work with changing data, encapsulating that data as member fields that are operated on by instance methods is the way to go.

For example, a Car class might have static numbeOfWheels() that always returns 4, but an instance numberOfFlatTires() that might return 0-4 depending on the state of that particular Car.

Inner classes are no different and the only difference between a static and non-static inner class is that the non-static can use the parent instance's members. This can be used to reduce complexity. You might have a looping operation that has a common parameter for the list and an individual parameter for the items. You could use a non-static inner class to encapsulate the operations on the item while referring to the common parameter in the parent class.

Enums are special in that each value is a single instance of a single type that all extend from a common abstract base class defined in the Enum class body. The Enum value is instantiated the first time it's used, but there will only ever be one instance per value.

Upvotes: 3

Blasanka
Blasanka

Reputation: 22437

First of all Declaring mean:

ClassName obj;

Simple meaning of instantiate is creating an object from class.

ClassName obj = new ClassName();

What is a object?

  • An instance of a class. From one class we can create many instances.
  • They are the basic runtime entities in in our program.
  • They may also represent user-defined data types such as lists and vectors.
  • Any programming problem is analyzed in terms of objects and nature of communication between them.

As a example:

//Define a reference(a variable) which can hold a `Person` obect.
Person p;
//Create a Person object(instantiate).
//new - use to allocate memory space for the new object
p = new Person();

What is a nested class?

A class that defined inside a class is called nested class. There 2 categories of nested classes.

  1. inner classes
  2. local classes
  3. annonymous classes

Inner class:

  • Inner class can only be accessed by the outer class. Not by any other class.
  • Inner class is a member of outer class.
  • Outer class can access inner class without importing.
  • Inner class can access any attribute or a method belong to outer directly.
  • Outer class cannot access directly to a inner class.

Example for a inner class:

class Outer{
   int i = 10;
   void main(){
      //instantiate inner class.
      Inner in = new Inner();
      in.show();
   }

   class Inner{
      void show(){
         System.out.print(i);
      }
   }
}

What is a local class?

Which are classes that are defined in a block.

Example:

public class{
  int i = 10;

  public main(){
     class A{
        void show(){
          System.out.println(i);
        }
     }

     //inside the method instantiate local class.
     A obj = new obj();
     obj.show();
  }
  //outside the main() -block(method)
  //inside another method instantiate local class.
  public test(){
    A obj = new A();
    obj.show();
  }
}

Upvotes: 16

Anzurio
Anzurio

Reputation: 17014

Instantiate is creating an instance of a class. I reckon this is not helpful without knowing what an instance is.

Let's say you have a class definition like:

public class Person
{
    private String name;
    public Person(String name) 
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
}

You make an instance of this class my calling its constructor and using the keyword new:

Person p = new Person("Hugh Laurie");

An instance of a class is a place in memory that contains the state (e.g., Person::name) of a given object which used as a template a class definition.

I want to further expand upon:

If you want to invoke the methods of local inner class you must instantiate this class

What this means is that, you need to have instantiated that class in order to use the above's example getName() method. After all, it is trying to access the state (name) of a given object in memory; without that object, there is no state.

Upvotes: 3

Ivan Pronin
Ivan Pronin

Reputation: 1916

Instantiate == create an instance == create an object of a class.

Upvotes: 6

Vitaliy Moskalyuk
Vitaliy Moskalyuk

Reputation: 2583

create an instance of the class by using "new" word

for example Car car = new Car();

Upvotes: 1

nhouser9
nhouser9

Reputation: 6780

To instantiate a class means to create an instance of the class. In other words, if you have a class like this:

public class Dog {
    public void bark() {
        System.out.println("woof");
    }
}

You would instantiate it like this:

Dog myDog = new Dog();

Instantiating is when you use the new keyword to actually create an object of your class.

Upvotes: 7

Related Questions