import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
public class comments extends Applet
{
/**
* @author Craig
*
* This class demonstrates use of:
* Line Comments
* Block Comments
* JavaDoc Comments.
*/
public class CommentsExample {
/**
* Program comments are nonexecuting,
* statements you add to a program
* documentation for the purpose
* of documentation.
*/
protected static String greeting = "Hi";
//Program comments are nonexecuting,
//statements you add to a program
private static String name = "Guys"; //documentation
public CommentsExample() {
//of documentation.()
super();
}
/**
* This is the entry point of the application.
* main() is executed first by the JVM.
*/
public void main(String[] args) {
/*
* Program comments are nonexecuting,
* statements you add to a program
* documentation for the purpose
* of documentation.
*/
System.out.println(greeting + " " + name);
}
}
}
Static declaration error - Inner Class
Page 1 of 15 Replies - 17401 Views - Last Post: 28 August 2006 - 05:09 AM
#1
Static declaration error - Inner Class
Posted 18 August 2006 - 01:55 AM
I keep getting this java error (inner class cannot have static declarations ) dont know why I just started learning Java dont know what im doing wrong. Thanks
Replies To: Static declaration error - Inner Class
#2
Re: Static declaration error - Inner Class
Posted 18 August 2006 - 05:32 AM
In your class CommentsExample, you have declared two variables as static - as mentioned by the error, inner classes cannot have static members. Static declarations are for top level entities, or for the class as a whole, not an instance.
#3
Re: Static declaration error - Inner Class
Posted 18 August 2006 - 07:27 AM
Actually an inner class can have a static variable if that inner class is declared as static. So the rule is, static variables can only be declarted in static or top-level classes.
#4
Re: Static declaration error - Inner Class
Posted 18 August 2006 - 07:36 AM
Excellent caveat! thanks for catching me on that, alcdotcom!
I should have been more careful!
#5
Re: Static declaration error - Inner Class
Posted 20 August 2006 - 09:01 PM
#6
Re: Static declaration error - Inner Class
Posted 28 August 2006 - 05:09 AM
benzspida, on 18 Aug, 2006 - 01:55 AM, said:
I keep getting this java error (inner class cannot have static declarations ) dont know why I just started learning Java dont know what im doing wrong. Thanks
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
public class comments extends Applet
{
/**
* @author Craig
*
* This class demonstrates use of:
* Line Comments
* Block Comments
* JavaDoc Comments.
*/
public class CommentsExample {
/**
* Program comments are nonexecuting,
* statements you add to a program
* documentation for the purpose
* of documentation.
*/
protected static String greeting = "Hi";
//Program comments are nonexecuting,
//statements you add to a program
private static String name = "Guys"; //documentation
public CommentsExample() {
//of documentation.()
super();
}
/**
* This is the entry point of the application.
* main() is executed first by the JVM.
*/
public void main(String[] args) {
/*
* Program comments are nonexecuting,
* statements you add to a program
* documentation for the purpose
* of documentation.
*/
System.out.println(greeting + " " + name);
}
}
}
Hai,
The problem why you are getting the error is, u are declaring the class as inner class. So the inner class is a member ( instance) variable (assume like that not the same) for the outer class. So it is like if u r declaring the instance methos in a class. Can u declare a static variable inside the method? can't obviously. So just like inside the instance declaration of a inner class you cannot declare static variables. So the solution is u have to make the class as static or you have to remove the static declaration of variables
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|