What is the exact meaning of instantiate in JAVA

后端 未结 7 1110
孤独总比滥情好
孤独总比滥情好 2021-01-01 03:37

I\'am a newbie in JAVA and this came across this word called. \"A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods o

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 04:16

    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();
      }
    }
    

提交回复
热议问题