• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we call an object in a method as local object and where it is stored -- on heap or on stack?  RSS feed

 
rajat kumar gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All,

I am having one doubt.. When we make some object in a class we call it instance variable but when we make an object in a method -- can we call this variable as local variable or local object?? Since local variables are stored on Stack, is this local object also stored on stack?? Since it is an object it should be stored on heap??

class A1{ }

class B1{
A1 a1 = new A1();// this is an instance variable and object also .. so it is stored on heap.. right??
public static void main(String [] args){
A1 a2=new A1(); // this is a local variable as it is in method ... but it is an object also.. so where it will be stored ???
}
}

thanks in advance...

No question is silly... Silly is what you believe something is silly....
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a local object. All objects reside on the heap. References to them can be stored in class-, instance- or local variables. Local variables are on the stack.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12156
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"austin india"

Please check your private messages regarding an important administrative matter.

-Andrew
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!