0

I have doubt in java object creation. The below code says that object created by new keyword and this keyword are same. But why we cant use this keyword to call non static methods of other class in main method but we can use object reference of new keyword to call methods of other class in main method. The output is same of below code that means the object created by both is same i think.

class A5 {
    void m() {
        System.out.println(this); //prints same reference ID
    }
    public static void main(String args[]) {
        A5 obj=new A5();
        System.out.println(obj); //prints the reference ID
        obj.m();
    }
}  
 output: A5@22b3ea59

A5@22b3ea59

Ayush
  • 87
  • 2
  • 9
  • Your question is the result of a misconception: `this` does not create an object, it is merely a reference to the current object. – Mark Rotteveel Aug 09 '18 at 15:19

1 Answers1

0

this keyword does not create new object. this keyword reference to current instance of class. So this keyword unders class A5 represents for a instance of A5 -> u can use this keyword to call A5 method only.