1

I just want to make clear that to call a function in two forms like

  1. By creating an object and calling the method by using that object.
  2. Without creating a object calling the function.

I mean for instance i have a class like

Class A{
  public int callMethod(){
     return 2;
  }
}

Now I am creating another class to call the method callMethod defined in the Class A

Class B {
  public static void main(String[] args) throws ParseException {

      A a = new A();
      //1st form to call the method
      int aa = a.callMethod()
      System.out.println(aa);

      //2nd form to call the method
      aa = new A().callMethod();
      System.out.println(aa);
  }
}

here in the first statement after creation of a object i am calling the callMethod() of class A by using the class object of A. And in the second time i am calling the method directly without creating the object class A. In the first form calling the method it is damn sure that we are creating the object and occupies some space in the memory for the object. Then what about the second form calling the method? Will it take any object creation? Which one is quicker? can anyone give me the clarifications on this.

Sreekanth
  • 534
  • 1
  • 5
  • 21
  • Well,new A() is itself creating an object in the heap!AIn't it!!! – Am_I_Helpful Jun 30 '14 at 06:26
  • 1
    "without creating the object" phrase is wrong. You are actually creating the object when you call new A() – small_ticket Jun 30 '14 at 06:29
  • [This answer](http://stackoverflow.com/a/23076023/1686291) may help you !! However its about passing object to a method, not invoking a method. But context is almost similar. – Not a bug Jun 30 '14 at 06:29
  • There is no difference (whatsoever here). As the local variable `a` is just a memory location on the stack to store the "pointer" to the object resulting from `new A()`. Also in the second case the temporary result of `new A()` is stored on the stack. – Joop Eggen Jun 30 '14 at 06:33
  • 1
    the keyword `new` always instantiates the object and returns the object reference. Its up to you if you want to assign it to a variable or not. – Shailesh Aswal Jun 30 '14 at 06:37

7 Answers7

3

When you use new keyword and constructor, in this case, new A(), it is creating a new object.

Wundwin Born
  • 3,467
  • 19
  • 37
2

And in the second time i am calling the method directly without creating the object class A.

That's not true - the object is still created, it only has no name you can refer to afterwards.

Smutje
  • 17,733
  • 4
  • 24
  • 41
1

Both of your ways are creating an instance of the object but in second case you don't have variable to point to the object if you want to access the second object later you can not do it

Ajay Soni
  • 26
  • 2
  • Why this is not posted as comment? Please edit and provide suitable points so as to mark this as answer! – Aditya Jun 30 '14 at 07:03
0

In the second code you are creating an instance of A and it is occupying space on the stack.

The first code will require that you instantiate A on the heap space (Unless you instantiate it from static code, for example:

static {
   A a = new A();
}

)

Ben
  • 10,020
  • 21
  • 94
  • 157
0

You are creating objects in the BOTH the methods. The statement

new A()

creates a new object and then you are calling a function. You should also know that objects DO NOT get different memory space for methods. All the objects of a class share the same memory space for methods. so it really doesnt matter about the space. If you want to call a function without using a object, you should make the function static, that way you cam call the function without actually using an object.

hope this helps.

Aradhna
  • 973
  • 10
  • 20
0

Read about stack space and heap space in java

In java we have references referring to objects on heap space,

In both cases, it is creating object on heap space but in first case you have reference stored on stack space with A a ("a" is reference)

So in same method or any other methos to which this reference is passed can refer to the original object (first object created) from heap space...

In second case you are calling methos directly on new obj which has no reference stored on stack space to refer it afterwards like in first case.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
user2408578
  • 464
  • 1
  • 4
  • 13
0

Both are same..!!

When you write

new A();

here Compiler calls the Non-parameterized constructor (if it is not written by programmer compiler calls the default constructor )

like:

 A()
{
 //i am default constructor
 super();
}

note: here super(); call the immediate super-class default constructor which is the Object class and object is created.

A a = new A();

here object is created and you assign that object to reference 'a';

learner
  • 365
  • 1
  • 3
  • 16