• Post Reply Bookmark Topic Watch Topic
  • New Topic

java codin help  RSS feed

 
benz spida
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:<br>
* Line Comments<br>
* Block Comments<br>
* JavaDoc Comments.<br>
*/
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);
}
}
}
 
Masoud Kalali
Author
Ranch Hand
Posts: 531
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to define the main method as static like :


you can not use an static member inside a none static method and vice-versa .
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Masoud Kalali:

you can not use an static member inside a none static method and vice-versa .


I'll agree with the "vice-versa", but nothing stops a non-static method from accessing a static member.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yepp...

for never forgetting a little hook:

if you couldn't call instance-vars in static methods you would never be able to run any OO-java program through main entry point:
public static void main (String args[]){...}
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbish.

Your issue is simple: an inner class cannot declare static members. This obviously requires a definition.

Inner class: A nested class that is not implicitly or explicitly declared static.

Nested class: A class whose declaration occurs within the body of another class or interface.

Top level class: A class that is not a nested class.

In your example the class "comment" is a top level class. It is not declared within the body of another class and is by definition not a nested class. The class "CommentsExample" is declared within the body of the class "comment". This makes CommentsExample a nested class by definition. Furthermore, CommentsExample is not explicitly declared static and is not implicitly static either making it an inner class by definition. One of the rules in the JLS is that an inner class may not declare static members. This includes methods and variables. Your static String "greeting" is a static member and since it is declared in CommentsExample, an inner class, an error is thrown.

You can solve this by either declaring CommentsExample static or by making CommentsExample a top level class instead of an inner class. The real question, however, is what are you attempting to do?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Masoud Kalali:
you need to define the main method as static like :


A main method should be static, however that is not at ALL the problem and would in fact be illegal and create another error.

Originally posted by Masoud Kalali:
you can not use an static member inside a none static method and vice-versa .


Nonsense.

Originally posted by manuel aldana:
yepp...

for never forgetting a little hook:

if you couldn't call instance-vars in static methods you would never be able to run any OO-java program through main entry point:
public static void main (String args[]){...}


Complete nonsense.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!