Java codin help

They have: 9 posts

Joined: Jun 2006

keep gettin 2 error messages

(1)The field greeting cannot be declared static; static fields can only be declared in static or top level types

this is what the code looks like

public class comment {
/**
* @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 for the purpose

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

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

The static field greeting is in an inner class CommentsExample. This isn't allowed in Java. To make it compile, either:

  • Declare CommentsExample as static as well: public static class CommentsExample {'
  • Declare a static field in the comment (top-level) class, not in CommentsExample.
  • Don't declare greeting as static.

Indent your code, btw, or use [code] tags when posting on the forum so whitespace won't get lost. Wink

Also, are you sure you need to comment that heavily? Even for an example... Smiling

They have: 2 posts

Joined: Jul 2006

Many tutorials are available at thesitewizard.com which may help with your script.
ac-webdesign.net

They have: 20 posts

Joined: Jul 2006

Abhishek Reddy wrote: The static field greeting is in an inner class CommentsExample. This isn't allowed in Java. To make it compile, either:

  • Declare CommentsExample as static as well: public static class CommentsExample {'
  • Declare a static field in the comment (top-level) class, not in CommentsExample.
  • Don't declare greeting as static.

Indent your code, btw, or use [code] tags when posting on the forum so whitespace won't get lost. Wink

Also, are you sure you need to comment that heavily? Even for an example... Smiling

I'm not totally up to snuff on Java 5 but when I tried your information it did compile but I got a runtime exception.

Exception in thread "main" java.lang.NoSuchMethodError: main

I'm pretty sure I have my classpath set right. I might be doing something wrong here but figured this info might help.

Johnny

They have: 5 posts

Joined: Jul 2006

Static class is a new feature of java 5. i think you should post error message.
You can get many java tutorials from www.developerzone.biz and www.javaworld.com.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.