• 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

static variables

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI folks

I had a bit of an epiphany and want to double check I got it right.

All objects created in:

public static void main(String...args){

}


are themselves static, right?

Mark
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean like this:

?

I would say the variable m is a local variable in main(). The created object is eligible to garbage collection after the method has finished if no other references have been created which are still alive. I think this is the same in a non-static method so there is nothing particular to objects created in a static method.

John
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question came from this problem in Sierra and Bates:



The correct answer says, in part:


What might be less clear is that you can still access the other Beta object through the static variable a2.b1 because it's static.



and my question is what the heck makes a2.b1 static?

 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The irritating thing here is that the instance variable a2 is used to access the static variable a2.b1. The compiler will replace the name of the instance variable a2 by the name of the class (Alpha). Therefore a2.b1 could be written as Alpha.b1. So b1 is a static variable of the class Alpha. The expression a2.b1 has nothing to do with the object referenced by a2. In fact you could even do a2 = null; and a2.b1 will still work.

John
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mark juszczec wrote:All objects created in:

public static void main(String...args){

}


are themselves static, right?



No. There is no such thing as a "static object". Only variables and methods can be declared as static. Did you have some particular feature in mind when you made that statement?
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mark juszczec wrote:and my question is what the heck makes a2.b1 static?



This declaration makes the "b1" variable static:
reply
    Bookmark Topic Watch Topic
  • New Topic