Accessing non-static members through the main method in Java











up vote
8
down vote

favorite
2












As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!










share|improve this question






















  • How did you come to the conclusion that the main method has access to instance variables and methods?
    – Laf
    Oct 25 '11 at 19:34















up vote
8
down vote

favorite
2












As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!










share|improve this question






















  • How did you come to the conclusion that the main method has access to instance variables and methods?
    – Laf
    Oct 25 '11 at 19:34













up vote
8
down vote

favorite
2









up vote
8
down vote

favorite
2






2





As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!










share|improve this question













As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 25 '11 at 18:04









Bhavesh

88228




88228












  • How did you come to the conclusion that the main method has access to instance variables and methods?
    – Laf
    Oct 25 '11 at 19:34


















  • How did you come to the conclusion that the main method has access to instance variables and methods?
    – Laf
    Oct 25 '11 at 19:34
















How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34




How did you come to the conclusion that the main method has access to instance variables and methods?
– Laf
Oct 25 '11 at 19:34












7 Answers
7






active

oldest

votes

















up vote
20
down vote



accepted










The main method does not have access to non-static members either.



public class Snippet
{
private String instanceVariable;
private static String staticVariable;

public String instanceMethod()
{
return "instance";
}

public static String staticMethod()
{
return "static";
}

public static void main(String args)
{
System.out.println(staticVariable); // ok
System.out.println(Snippet.staticMethod()); // ok

System.out.println(new Snippet().instanceMethod()); // ok
System.out.println(new Snippet().instanceVariable); // ok

System.out.println(Snippet.instanceMethod()); // wrong
System.out.println(instanceVariable); // wrong
}
}





share|improve this answer




























    up vote
    5
    down vote













    By creating an object of that class.



    public class Test {
    int x;

    public static void main(String args) {
    Test t = new Test();
    t.x = 5;
    }
    }





    share|improve this answer




























      up vote
      2
      down vote













      YourClass inst = new YourClass();
      inst.nonStaticMethod();
      inst.nonStaticField = 5;





      share|improve this answer




























        up vote
        2
        down vote













        The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.



        This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.



        In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.



        To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.






        share|improve this answer




























          up vote
          1
          down vote













          Through the reference to the object.



          public class A {
          private String field;


          public static void main(String... args) {
          //System.out.println(field); <-- can not do this
          A a = new A();
          System.out.println(a.field); //<-- access instance var field that belongs to the instance a
          }
          }





          share|improve this answer




























            up vote
            1
            down vote













            You have to instantiate the object you wish to access.



            For example



            public class MyClass{

            private String myData = "data";

            public String getData(){
            return myData;
            }

            public static void main(String args){
            MyClass obj = new MyClass();
            System.out.println(obj.getData());
            }
            }





            share|improve this answer




























              up vote
              0
              down vote













              You must create an instance of the class in order to reference instance variables & methods.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7893755%2faccessing-non-static-members-through-the-main-method-in-java%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                7 Answers
                7






                active

                oldest

                votes








                7 Answers
                7






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                20
                down vote



                accepted










                The main method does not have access to non-static members either.



                public class Snippet
                {
                private String instanceVariable;
                private static String staticVariable;

                public String instanceMethod()
                {
                return "instance";
                }

                public static String staticMethod()
                {
                return "static";
                }

                public static void main(String args)
                {
                System.out.println(staticVariable); // ok
                System.out.println(Snippet.staticMethod()); // ok

                System.out.println(new Snippet().instanceMethod()); // ok
                System.out.println(new Snippet().instanceVariable); // ok

                System.out.println(Snippet.instanceMethod()); // wrong
                System.out.println(instanceVariable); // wrong
                }
                }





                share|improve this answer

























                  up vote
                  20
                  down vote



                  accepted










                  The main method does not have access to non-static members either.



                  public class Snippet
                  {
                  private String instanceVariable;
                  private static String staticVariable;

                  public String instanceMethod()
                  {
                  return "instance";
                  }

                  public static String staticMethod()
                  {
                  return "static";
                  }

                  public static void main(String args)
                  {
                  System.out.println(staticVariable); // ok
                  System.out.println(Snippet.staticMethod()); // ok

                  System.out.println(new Snippet().instanceMethod()); // ok
                  System.out.println(new Snippet().instanceVariable); // ok

                  System.out.println(Snippet.instanceMethod()); // wrong
                  System.out.println(instanceVariable); // wrong
                  }
                  }





                  share|improve this answer























                    up vote
                    20
                    down vote



                    accepted







                    up vote
                    20
                    down vote



                    accepted






                    The main method does not have access to non-static members either.



                    public class Snippet
                    {
                    private String instanceVariable;
                    private static String staticVariable;

                    public String instanceMethod()
                    {
                    return "instance";
                    }

                    public static String staticMethod()
                    {
                    return "static";
                    }

                    public static void main(String args)
                    {
                    System.out.println(staticVariable); // ok
                    System.out.println(Snippet.staticMethod()); // ok

                    System.out.println(new Snippet().instanceMethod()); // ok
                    System.out.println(new Snippet().instanceVariable); // ok

                    System.out.println(Snippet.instanceMethod()); // wrong
                    System.out.println(instanceVariable); // wrong
                    }
                    }





                    share|improve this answer












                    The main method does not have access to non-static members either.



                    public class Snippet
                    {
                    private String instanceVariable;
                    private static String staticVariable;

                    public String instanceMethod()
                    {
                    return "instance";
                    }

                    public static String staticMethod()
                    {
                    return "static";
                    }

                    public static void main(String args)
                    {
                    System.out.println(staticVariable); // ok
                    System.out.println(Snippet.staticMethod()); // ok

                    System.out.println(new Snippet().instanceMethod()); // ok
                    System.out.println(new Snippet().instanceVariable); // ok

                    System.out.println(Snippet.instanceMethod()); // wrong
                    System.out.println(instanceVariable); // wrong
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 25 '11 at 18:07









                    tjg184

                    3,44111644




                    3,44111644
























                        up vote
                        5
                        down vote













                        By creating an object of that class.



                        public class Test {
                        int x;

                        public static void main(String args) {
                        Test t = new Test();
                        t.x = 5;
                        }
                        }





                        share|improve this answer

























                          up vote
                          5
                          down vote













                          By creating an object of that class.



                          public class Test {
                          int x;

                          public static void main(String args) {
                          Test t = new Test();
                          t.x = 5;
                          }
                          }





                          share|improve this answer























                            up vote
                            5
                            down vote










                            up vote
                            5
                            down vote









                            By creating an object of that class.



                            public class Test {
                            int x;

                            public static void main(String args) {
                            Test t = new Test();
                            t.x = 5;
                            }
                            }





                            share|improve this answer












                            By creating an object of that class.



                            public class Test {
                            int x;

                            public static void main(String args) {
                            Test t = new Test();
                            t.x = 5;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 25 '11 at 18:06









                            Oliver Charlesworth

                            225k25465593




                            225k25465593






















                                up vote
                                2
                                down vote













                                YourClass inst = new YourClass();
                                inst.nonStaticMethod();
                                inst.nonStaticField = 5;





                                share|improve this answer

























                                  up vote
                                  2
                                  down vote













                                  YourClass inst = new YourClass();
                                  inst.nonStaticMethod();
                                  inst.nonStaticField = 5;





                                  share|improve this answer























                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    YourClass inst = new YourClass();
                                    inst.nonStaticMethod();
                                    inst.nonStaticField = 5;





                                    share|improve this answer












                                    YourClass inst = new YourClass();
                                    inst.nonStaticMethod();
                                    inst.nonStaticField = 5;






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Oct 25 '11 at 18:07









                                    xthexder

                                    1,421818




                                    1,421818






















                                        up vote
                                        2
                                        down vote













                                        The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.



                                        This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.



                                        In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.



                                        To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.






                                        share|improve this answer

























                                          up vote
                                          2
                                          down vote













                                          The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.



                                          This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.



                                          In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.



                                          To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.






                                          share|improve this answer























                                            up vote
                                            2
                                            down vote










                                            up vote
                                            2
                                            down vote









                                            The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.



                                            This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.



                                            In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.



                                            To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.






                                            share|improve this answer












                                            The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.



                                            This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.



                                            In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.



                                            To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Oct 25 '11 at 18:12









                                            Desmond Zhou

                                            1,1681017




                                            1,1681017






















                                                up vote
                                                1
                                                down vote













                                                Through the reference to the object.



                                                public class A {
                                                private String field;


                                                public static void main(String... args) {
                                                //System.out.println(field); <-- can not do this
                                                A a = new A();
                                                System.out.println(a.field); //<-- access instance var field that belongs to the instance a
                                                }
                                                }





                                                share|improve this answer

























                                                  up vote
                                                  1
                                                  down vote













                                                  Through the reference to the object.



                                                  public class A {
                                                  private String field;


                                                  public static void main(String... args) {
                                                  //System.out.println(field); <-- can not do this
                                                  A a = new A();
                                                  System.out.println(a.field); //<-- access instance var field that belongs to the instance a
                                                  }
                                                  }





                                                  share|improve this answer























                                                    up vote
                                                    1
                                                    down vote










                                                    up vote
                                                    1
                                                    down vote









                                                    Through the reference to the object.



                                                    public class A {
                                                    private String field;


                                                    public static void main(String... args) {
                                                    //System.out.println(field); <-- can not do this
                                                    A a = new A();
                                                    System.out.println(a.field); //<-- access instance var field that belongs to the instance a
                                                    }
                                                    }





                                                    share|improve this answer












                                                    Through the reference to the object.



                                                    public class A {
                                                    private String field;


                                                    public static void main(String... args) {
                                                    //System.out.println(field); <-- can not do this
                                                    A a = new A();
                                                    System.out.println(a.field); //<-- access instance var field that belongs to the instance a
                                                    }
                                                    }






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Oct 25 '11 at 18:07









                                                    Bhesh Gurung

                                                    40.8k2074128




                                                    40.8k2074128






















                                                        up vote
                                                        1
                                                        down vote













                                                        You have to instantiate the object you wish to access.



                                                        For example



                                                        public class MyClass{

                                                        private String myData = "data";

                                                        public String getData(){
                                                        return myData;
                                                        }

                                                        public static void main(String args){
                                                        MyClass obj = new MyClass();
                                                        System.out.println(obj.getData());
                                                        }
                                                        }





                                                        share|improve this answer

























                                                          up vote
                                                          1
                                                          down vote













                                                          You have to instantiate the object you wish to access.



                                                          For example



                                                          public class MyClass{

                                                          private String myData = "data";

                                                          public String getData(){
                                                          return myData;
                                                          }

                                                          public static void main(String args){
                                                          MyClass obj = new MyClass();
                                                          System.out.println(obj.getData());
                                                          }
                                                          }





                                                          share|improve this answer























                                                            up vote
                                                            1
                                                            down vote










                                                            up vote
                                                            1
                                                            down vote









                                                            You have to instantiate the object you wish to access.



                                                            For example



                                                            public class MyClass{

                                                            private String myData = "data";

                                                            public String getData(){
                                                            return myData;
                                                            }

                                                            public static void main(String args){
                                                            MyClass obj = new MyClass();
                                                            System.out.println(obj.getData());
                                                            }
                                                            }





                                                            share|improve this answer












                                                            You have to instantiate the object you wish to access.



                                                            For example



                                                            public class MyClass{

                                                            private String myData = "data";

                                                            public String getData(){
                                                            return myData;
                                                            }

                                                            public static void main(String args){
                                                            MyClass obj = new MyClass();
                                                            System.out.println(obj.getData());
                                                            }
                                                            }






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Oct 25 '11 at 18:09









                                                            Chris Thompson

                                                            28.7k96896




                                                            28.7k96896






















                                                                up vote
                                                                0
                                                                down vote













                                                                You must create an instance of the class in order to reference instance variables & methods.






                                                                share|improve this answer



























                                                                  up vote
                                                                  0
                                                                  down vote













                                                                  You must create an instance of the class in order to reference instance variables & methods.






                                                                  share|improve this answer

























                                                                    up vote
                                                                    0
                                                                    down vote










                                                                    up vote
                                                                    0
                                                                    down vote









                                                                    You must create an instance of the class in order to reference instance variables & methods.






                                                                    share|improve this answer














                                                                    You must create an instance of the class in order to reference instance variables & methods.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Aug 20 at 17:36









                                                                    Rudra

                                                                    408




                                                                    408










                                                                    answered Oct 25 '11 at 18:07









                                                                    dbreaux

                                                                    4,12911449




                                                                    4,12911449






























                                                                        draft saved

                                                                        draft discarded




















































                                                                        Thanks for contributing an answer to Stack Overflow!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.





                                                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                        Please pay close attention to the following guidance:


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function () {
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7893755%2faccessing-non-static-members-through-the-main-method-in-java%23new-answer', 'question_page');
                                                                        }
                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        404 Error Contact Form 7 ajax form submitting

                                                                        How to know if a Active Directory user can login interactively

                                                                        TypeError: fit_transform() missing 1 required positional argument: 'X'