Results 1 to 10 of 10
  1. #1
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default The following code showing error . . . . I just want to know the reason .

    class This1{
    void show(){
    System.out.println("abc");
    }
    }
    public class ThisTest { This1 t1=new This1();
    public static void main(String[] args) {
    t1.show();
    } showing compile time error
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: The following code showing error . . . . I just want to know the reason .

    Make your This1 t1 static.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: The following code showing error . . . . I just want to know the reason .

    After adding static keyword to This1 t1=new This1(); program executed correctly but till now i don't understand why we are using static keyword there . Is it compulsory to make static any thing out side the main method. Thanks for response.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: The following code showing error . . . . I just want to know the reason .

    You have to fully understand what static means. I suggest you read your notes, textbook or search online.

    Static means that it belongs to the class and not to an instance (object) of that class. In your code you have declared the t1 variable to be an instance variable of the ThisTest class which means you need to create an instance of that class to access the t1 variable.

    Another way to look at it is to remember that the main method can be moved out of your ThisTest class into any other class.
    Java Code:
    class Junky {
        public static void main(String[] args) {
            t1.show();
        }
    }
    You get the exact same problem since my class has zero information about the t1 variable.

  5. #5
    rajpan06 is offline Member
    Join Date
    May 2013
    Posts
    10
    Rep Power
    0

    Default Re: The following code showing error . . . . I just want to know the reason .

    class This1{
    void show(){
    System.out.println("abc");
    }
    }
    public class ThisTest {
    static This1 t1=new This1();
    public static void main(String[] args) {
    t1.show();
    }
    }
    the above code not showing any error . But if i write like this. . class This1{
    void show(){
    System.out.println("abc");
    }
    }
    public class ThisTest {
    This1 t1=new This1();
    public static void main(String[] args) {
    t1.show();
    }
    }
     it is showing error . . 
    Last edited by rajpan06; 05-28-2013 at 07:15 AM.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: The following code showing error . . . . I just want to know the reason .

    See my first reply (reply #2).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: The following code showing error . . . . I just want to know the reason .

    Quote Originally Posted by rajpan06 View Post
     it is showing error . . 
    Exactly as it has already been explained.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: The following code showing error . . . . I just want to know the reason .

    I'm afraid the OP doesn't understand what the keyword 'static' means; as many others do, they sprinkle in the keyword 'static' here and there until the compiler keeps its mouth shut. The answer is in the tutorials (as always).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: The following code showing error . . . . I just want to know the reason .

    Which is why I suggested they read about static and even provided an explanation. It obviously went whooooosh!

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: The following code showing error . . . . I just want to know the reason .

    Quote Originally Posted by Junky View Post
    Which is why I suggested they read about static and even provided an explanation. It obviously went whooooosh!
    You must've accidentally pressed the 'invisible ink' button before you submitted your reply; it happens to me all the time ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 6
    Last Post: 05-26-2013, 02:53 AM
  2. Replies: 3
    Last Post: 09-20-2012, 11:11 AM
  3. Replies: 1
    Last Post: 01-31-2012, 08:48 AM
  4. Code stops for no reason
    By Bagzli in forum New To Java
    Replies: 9
    Last Post: 03-03-2011, 03:56 AM
  5. [SOLVED] Can't pinpoint reason for this error..
    By Reiyn in forum New To Java
    Replies: 6
    Last Post: 03-30-2009, 06:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •