Difference between Static methods and Instance methods

前端 未结 10 1093
青春惊慌失措
青春惊慌失措 2020-11-22 05:30

I was just reading over the text given to me in my textbook and I\'m not really sure I understand what it is saying. It\'s basically telling me that static methods or class

10条回答
  •  醉话见心
    2020-11-22 06:09

    Methods and variables that are not declared as static are known as instance methods and instance variables. To refer to instance methods and variables, you must instantiate the class first means you should create an object of that class first.For static you don't need to instantiate the class u can access the methods and variables with the class name using period sign which is in (.)

    for example:

    Person.staticMethod();           //accessing static method.
    

    for non-static method you must instantiate the class.

    Person person1 = new Person();   //instantiating
    person1.nonStaticMethod();       //accessing non-static method.
    

提交回复
热议问题