• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does static modifier required ???  RSS feed

 
Peter Sorotokin
Ranch Hand
Posts: 36
1
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code :



For examle in line 126 method outputGrades() did not declared as static . Does it posible that a non static method is used as in line 37 ???
I think that if i want to use this method like line 37 then i shoud use static modifier in its declaration in line 126 . Is this true ??? If not , Why ??? Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to compile and run your program? Did you get any errors?

Why do you think you need to make the method in line 126 static?

Do you understand what static means? See: Understanding Instance and Class Members in Oracle's Java Tutorials for an explanation.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some very dubious things on JavaRanch, but surely the most dubious is the Campbell Ritchie classification of Methods. Look at your method in view of that classification, and you find your method reads information from an instance field, ie information out of the object.

The default is for members to be not static. If you are tempted to make something static, you need a good explanation for it.
 
Peter Sorotokin
Ranch Hand
Posts: 36
1
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So in line 3 if we omit static identifier , the code does not compile . Why ??? Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the main method, from which you call the average method, is static.

static means that the method or variable belongs to the class. If a method is not static, then you need an instance of the class (an object) to call it on.

I already gave you the link to this page in Oracle's tutorials, which explains it: Understanding Instance and Class Members.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!