• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Java Static method

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have written the following code:

I looked on web, it says that i can call a static variable from a non-static method, but i am not able to run the code, it's giving an error.
 
Marshal
Posts: 76834
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You are calling the setSalary method on the class name not the object name.
There are several other design and style errors, e.g. capital letters missed out (which caused your error message), public fields.
 
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
Welcome to the Ranch.

You have made the salary member variable in class emp static. That means that there is only one copy of the variable which is shared between all emp objects. In other words, all employees have the same salary, and if you call setSalary on one emp object, then it changes for all employees. Is that really what you meant to do?

Remove the static from the declaration of the variable salary in line 3.

Also, line 22 is wrong, you want to call setSalary on the object that you've just created, and not on the emp class.

For more information about what static means, see: Understanding Class Members in Oracle's Java Tutorials.

(Note: A name such as emp is a bad name for a class. Stick to the common conventions and start class names with an upper-case letter, for example: Emp, or Employee).
 
Greenhorn
Posts: 7
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Welcome to Ranch

This might be helpful




Das
 
Marshal
Posts: 8513
610
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Das Verma wrote:This might be helpful


You missed one important concept: to keep all instance variables private, always, except the cases when it really needs to be otherwise. This is not a case with both variables OP has.

[Addition] Actually this version worse than OP's initial, I'm afraid.
 
Dasrath Verma
Greenhorn
Posts: 7
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Liutauras Vilda

Thanks for this it was just by my mistake.

das
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic