What is the exact meaning of instantiate in JAVA

后端 未结 7 1069
孤独总比滥情好
孤独总比滥情好 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:28

    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.

    0 讨论(0)
提交回复
热议问题