Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does static modifier required ???

 
Ranch Hand
Posts: 36
1
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic