Results 1 to 10 of 10
- 05-26-2013, 06:12 PM #1
Member
- Join Date
- May 2013
- Posts
- 10
- Rep Power
- 0
- 05-26-2013, 07:48 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: The following code showing error . . . . I just want to know the reason .
Make your This1 t1 static.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-27-2013, 04:21 AM #3
Member
- Join Date
- May 2013
- Posts
- 10
- Rep Power
- 0
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.
- 05-27-2013, 05:26 AM #4
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(); } }
- 05-28-2013, 08:12 AM #5
Member
- Join Date
- May 2013
- Posts
- 10
- Rep Power
- 0
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 08:15 AM.
- 05-28-2013, 08:44 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: The following code showing error . . . . I just want to know the reason .
See my first reply (reply #2).
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-28-2013, 09:43 AM #7
- 05-28-2013, 09:58 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-28-2013, 10:01 AM #9
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!
- 05-28-2013, 10:14 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: The following code showing error . . . . I just want to know the reason .
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
The following code showing run time error . . . . I want to know the reason
By rajpan06 in forum New To JavaReplies: 6Last Post: 05-26-2013, 03:53 AM -
cobertura code coverage showing classnotfound error for log4j.jar in hudson
By spsathees in forum Apache AntReplies: 3Last Post: 09-20-2012, 12:11 PM -
Plz help for some reason this code is not compiling
By webslinger55 in forum JDBCReplies: 1Last Post: 01-31-2012, 09:48 AM -
Code stops for no reason
By Bagzli in forum New To JavaReplies: 9Last Post: 03-03-2011, 04:56 AM -
[SOLVED] Can't pinpoint reason for this error..
By Reiyn in forum New To JavaReplies: 6Last Post: 03-30-2009, 07:39 PM
Bookmarks