Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic