8 Replies Latest reply: May 8, 2012 7:50 AM by 796440 RSS

    Doubt regarding static functionality of main() in Java

    925962
      Doubt regarding static functionality of main() in Java
      -----------------------------------------------------------------------

      We know that:

      1. A static method can access only static data. It can not access non-static data (instance variables)

      2. A static method can call only other static methods and can not call a non-static method from it.

      However, main method in Java is declared static but it can call non static methods as well as access non static data.
      How so?
        • 1. Re: Doubt regarding static functionality of main() in Java
          gimbal2
          Sarthak Gupta wrote:
          However, main method in Java is declared static but it can call non static methods as well as access non static data.
          Prove it. Create a small application that does exactly that and post the code.

          You're wrong. So I want to see what you're really doing that makes you draw this conclusion.
          • 2. Re: Doubt regarding static functionality of main() in Java
            925962
            class Box     {
                 double length, width, height;
            }

            class BoxDemo     {
                 public static void main(String [] args)     {
                      Box mybox = new Box();
                      double vol;
                      
                      //assign values to mybox instatnce variables
                      mybox.length = 10;
                      mybox.width = 20;
                      mybox.height = 15;                    
                      
                      //Compute and display Volume
                      vol = mybox.length * mybox.width * mybox.height;
                      
                      System.out.println("The volume is :" + vol);
                      
                      return;
                 }
            }     

            Here we are able to access variables of Box object which are instatnce variables and are not static? Similarly:

            class Box     {
                 double length, width, height;
                 
                 double volume() {
                      return length * width * height;
                 }
                 
                 void setValue(double l, double w, double h)     {
                      length = l;
                      width = w;
                      height = h;
                 }
            }

            class BoxDemo5     {
                 public static void main(String [] args)     {
                      Box mybox1 = new Box();
                      Box mybox2 = new Box();
                      double vol;
                      
                      //assign values to mybox instatnce variables
                      mybox1.setValue(10, 20, 15);
                      mybox2.setValue(5, 20, 4);                    
                      
                      //Compute and display Volume
                      vol = mybox1.volume();
                      System.out.println("The volume of mybox1 is :" + vol);
                      
                      vol = mybox2.volume();
                      System.out.println("The volume of mybox2 is :" + vol);
                      return;
                 }
            }          

            Here we are accessing volume() method which again is not static.
            • 3. Re: Doubt regarding static functionality of main() in Java
              EJP
              We know that:
              We know this how?
              1. A static method can access only static data.
              False.
              2. A static method can call only other static methods and can not call a non-static method from it.
              False.
              However, main method in Java is declared static but it can call non static methods as well as access non static data.
              How so?
              Because your assumptions are false.

              Look at what practically any main() method actually does, and reconsider. Start with your own above.
              • 4. Re: Doubt regarding static functionality of main() in Java
                800268
                Sarthak Gupta wrote:
                Here we are able to access variables of Box object which are instatnce variables and are not static? Similarly:
                Here we are accessing volume() method which again is not static.
                Congrats, now you know your previous point 2 was wrong. Back to the tutorial http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
                which states (bolding mine):
                Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
                • 5. Re: Doubt regarding static functionality of main() in Java
                  gimbal2
                  Sarthak Gupta wrote:
                  Here we are accessing volume() method which again is not static.
                  No duh, because you are making object instances and calling the method on that.

                  You can't get anywhere by making assumptions and just trying stuff. You really have to do some reading and actually understand what these concepts are. What is a class? What is an object? What does the static keyword actually do? WHY do you need to create object instances to be able to call their (non-static) methods? I'm pretty sure you can't answer most of these very basic questions yet.

                  Taking your initial question literally, this is what you should have posted and that wouldn't have worked:
                  public class Test {
                    public void volume(){
                      System.out.println("Volume() called");
                    }
                  
                    public static void main(String[] args){
                      volume();  // compile error, volume is not static
                    }
                  }
                  But yeah, if you change that to this which matches the code you actually posted:
                  public class Test {
                    public void volume(){
                      System.out.println("Volume() called");
                    }
                  
                    public static void main(String[] args){
                     Test test = new Test(); 
                     test.volume();  // now its okay
                    }
                  }
                  then it works, because it is supposed to work.
                  • 6. Re: Doubt regarding static functionality of main() in Java
                    925962
                    Ok, I think I have got some Idea which I will try to put here. Correct me if I am wrong. Thanks in Advance.

                    Basically, a static method (or class method) can access instance variables and instance method - however to do that it should use an object reference. It can't access the instance variables and methods directly.

                    This is because, class definition is actually a framework which has no 'PHYSICAL EXISTENCE" in the memory as such. An object on the other hand has a physical existence in the memory. Therefore, a static method can't access the instance method and variable directly because if suppose the object of the class has not been created via new(), there is no existence of the object and correspondingly, no existence of its instance variables and methods. Thus there is no way, non static members can be accessed.

                    When i read the following two statements (as in my first post of this thread):

                    1. A static method can access only static data. It can not access non-static data (instance variables)
                    2. A static method can call only other static methods and can not call a non-static method from it.

                    I assumed that there is no possible way to access non-static members at all (directly or via object reference). I was wrong to think that.

                    I think I got the concept now.

                    Edited by: Sarthak Gupta on May 8, 2012 4:27 AM
                    • 7. Re: Doubt regarding static functionality of main() in Java
                      EJP
                      This is because, class definition is actually a framework which has no 'PHYSICAL EXISTENCE" in the memory as such.
                      False. Where do you think the static variables get stored, and the code, and the class name? Nowhere?
                      An object on the other hand has a physical existence in the memory.
                      True. So does a class.
                      Therefore, a static method can't access the instance method and variable directly because if suppose the object of the class has not been created via new(), there is no existence of the object and correspondingly, no existence of its instance variables and methods. Thus there is no way, non static members can be accessed.
                      Now this is starting to make sense. What you are struggling to say here is that a static method cannot access non-static members or methods of its own class without using a reference to an instance of that class.
                      I think I got the concept now.
                      Not quite. You are overcomplicating it. It is as simple as this: there is no 'this' inside a static method, so nothing that requires an implicit dereference of 'this' can be accessed from within a static method.
                      • 8. Re: Doubt regarding static functionality of main() in Java
                        796440
                        Sarthak Gupta wrote:
                        Ok, I think I have got some Idea which I will try to put here. Correct me if I am wrong. Thanks in Advance.

                        Basically, a static method (or class method) can access instance variables and instance method - however to do that it should use an object reference. It can't access the instance variables and methods directly.

                        This is because, class definition is actually a framework which has no 'PHYSICAL EXISTENCE" in the memory as such.
                        "Physical existence" and "memory" have nothing to do with it. It's as simple as this: The only way to refer to a non-static member M of class C is through reference to an object of type C, such as cReference.someMember. When you don't qualify it with a leading "cReference." the implied reference is "this." In a static context, there is no "this." So, no explicit "cReference" and no explicit or implicit "this" means no access to a non-static member.