• Post Reply Bookmark Topic Watch Topic
  • New Topic

Isn't this a local objects and why does it stlill exist?  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reviewing a simple Swing sample.
The JTable variable (myTable) is an instance variable.

Within the constructor SimpleJTable() is the local variable myPane which is a JScrollPane object.

Once the constructor is done executing, shouldn't the JScrollPane object go away since it is a local variable and prevent the table from being created?


 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote:Once the constructor is done executing, shouldn't the JScrollPane object go away since it is a local variable and prevent the table from being created?

The variable myTable is declated as static. It never goes away. It is initialized before any constructor of SimpleJTable class runs. It holds its value even if there are no live SimpleJTable object.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Tom Landry wrote:Once the constructor is done executing, shouldn't the JScrollPane object go away since it is a local variable and prevent the table from being created?

The variable myTable is declated as static. It never goes away. It is initialized before any constructor of SimpleJTable class runs. It holds its value even if there are no live SimpleJTable object.


Are you saying that since myTable is an instance variable that any value passed to it (even if a local object) will still exist even after the local object has gone away?

If so, is it because a copy of the object is being used and stored within the instance variable even though the local copy is gone?
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell me if this is on the right track.

In the constructor myPane is instantiated as a JScrollPane object.
The add(myPane) call within the constructor passes a copy of the object which is stored in a local variable within the JPanel object.
Since its own copy is now stored the local copy can go away.

Basically it was just built within the constructor and discarded after sending a copy to the main object.

Sound about right?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote:Are you saying that since myTable is an instance variable that any value passed to it (even if a local object) will still exist even after the local object has gone away?

No, he's saying the precise opposite. Your variable is static, and therefore NOT an instance variable.

PS: I was sad to hear of your demise. 'America's team' were never the same after you.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!