#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,534
    Rep Power
    595

    Static References Question


    I have a class that contains static objects. I have never really understood the concept well and recently I encountered a puzzling, to me, syntax. I tried to reference a couple of those static objects which resulted in my IDE complaining that I need to reference them in a static way. That much I understood:
    Code:
    if (SearchFrame.getInstance().prevDate.equals(SearchFrame.getInstance().currDate)) {
    However, the proposed fix puzzles me:
    Code:
    SearchFrame.getInstance();
    if (SearchFrame.prevDate.equals(SearchFrame.currDate)) {
    Can someone explain what this is doing and why it is different? Is this creating an implicit object with the same name as the class? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    SearchFrame.prevDate
    The syntax is: Classname . static Item

    SearchFrame is the name of a class
    prevDate is a static variable in the SearchFrame class

    SearchFrame.getInstance().prevDate
    What does the static method: getInstance() return?
    An reference to an instance of a class can be used to access a static item in the class.

    Code:
          TestClass.item = 22;
          TestClass tc = new TestClass();
          System.out.println(tc.item + " " + TestClass.item);       //  Use reference vs classname
    
    
        }	   // end main()
    
       static class TestClass {
         static int item = 99;
       }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,534
    Rep Power
    595
    Thanks for the reply but I am still not comprehending. The method getInstance() returns the instance of the SearchFrame class (that class permits only 1 instance of itself). Thus SearchFrame.getInstance().currDate should be the value of that instance's object but the fact that currDate is static changes how it can be referenced somehow and for some reason.

    As for the IDE's proposed fix, I have no clue what SearchFrame.getInstance() does when it is not set to any local variable. Yet referencing SearchFrame.currDate, rather than producing an error, works after that is executed.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    classname.variableName is the standard way to access a static public class variable.
    See Math class's PI variable for example.
    classname.methodName() is how to call a static method. See the Integer class or Math class for several examples.

    It also works with a reference to a class in place of the classname. See the sample code I posted.


    Do some research on what it means for a variable to be static.

IMN logo majestic logo threadwatch logo seochat tools logo