• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Does static modifier required ???

 
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Marshal
Posts: 69829
278
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Hey cool! They got a blimp! But I have a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic