5 Replies - 19149 Views - Last Post: 28 August 2006 - 05:09 AM Rate Topic: -----

#1 benzspida  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-July 06

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



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);
		}
	}
}






Is This A Good Question/Topic? 0
  • +

Replies To: Static declaration error - Inner Class

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

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.
Was This Post Helpful? 0
  • +
  • -

#3 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

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.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

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!
Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Static declaration error - Inner Class

Posted 20 August 2006 - 09:01 PM

View PostAmadeus, on 18 Aug, 2006 - 07:36 AM, said:

Excellent caveat! thanks for catching me on that, alcdotcom! :) I should have been more careful!

No problem. I hope you'll do the same for me. :)
Was This Post Helpful? 0
  • +
  • -

#6 prakash.skp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 02-August 06

Re: Static declaration error - Inner Class

Posted 28 August 2006 - 05:09 AM

View Postbenzspida, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1