Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

static reference variable

 
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MyClass {
static int a =4;
static MyClass ref;


....
}

Here 'ref' is a static reference variable.
What does that mean??
What is the benefit to declare static reference variable?
 
Saloon Keeper
Posts: 14321
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static means that the variable is part of the declaring class, rather than an instance of that class. You generally need static variables when you have information that should be shared between different instances of that class, or when the variable needs to be accessible globally, in any part of your program.
 
ankit maini
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Static means that the variable is part of the declaring class, rather than an instance of that class. You generally need static variables when you have information that should be shared between different instances of that class, or when the variable needs to be accessible globally, in any part of your program.




Yes i understand that static variable is class variable and shared between different instances of class, but I still don't get the idea of static reference variable ... can you give some example of static reference variable.
 
Stephan van Hulst
Saloon Keeper
Posts: 14321
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources. Where did you learn about "static reference variables"?

If I understand what you mean correctly, they are no different than a static primitive variable, except they hold references to objects.
 
ankit maini
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Please QuoteYourSources. Where did you learn about "static reference variables"?

If I understand what you mean correctly, they are no different than a static primitive variable, except they hold references to objects.




OK i have read this from SCJP - 'khalid mughal' book. Th actual code is here.

 
Stephan van Hulst
Saloon Keeper
Posts: 14321
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the main function, you can see that a new MyClass instance is being assigned to ref. This is no different from a non-static variable. The difference is that this new instance will now be accessible anywhere in the package. I could write a class as such:
See how I am able to access ref without having an object to get it from?
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic