Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling static methods/fields  RSS feed

 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again! I´m currently stuck on a project. Here´s what I should do:

1) Copy the Employee.java file from the java1_Lesson14 project to the java1_Project14 project. First, use what you've learned about encapsulation to protect your data.
2) Use a call to the System.out.println() method to display in the console the names and values of all of the instance variables in each instance of the Employee class. Also print to the console the value of any static variables.
3) Note that, if you access a static variable via an instance, Eclipse will warn you that this is not optimal behavior. Use the correct form for accessing and displaying any static information.


I think I did the encapsulation part right. The problem now is the warnings messages Im getting from Eclipse.

On line 20 I get the following message: The static method setTopSalary(int) from the type Employee should be accessed in a static way.
And on line 24: The static field Employee.topSalary should be accessed in a static way.

Can anyone shed a light on how do I fix this?

Thanks a lot!

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just a warning. But when accessing static members, it's best to use the class name as a prefix.

So: Employee.setTopSalary(199000);

What you are doing will work, but is considered poor form.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Why are you bothering to override the default value? Just use the desired value to being with.
 
Thiago Braun
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:That's just a warning. But when accessing static members, it's best to use the class name as a prefix.

So: Employee.setTopSalary(199000);

What you are doing will work, but is considered poor form.


Yes, it´s working, but I need to fix it. It´s already set to Employee.setTopSalary(199000), is it not? Also, when I print the values, I get 199001 and not 199000. I think that´s because I set e2´s salary, wich is an instance of Employee, to 199001.


Bear Bibeault wrote:P.S. Why are you bothering to override the default value? Just use the desired value to being with.


There are different instances of the Employee class with different salaries and from what I´ve understood, they want me to print to the console the value of all instance variables. In this case, I need to print e2 salary, wich is 199001. The value is actually showing correctly on the console, but I need to fix the error message.

Thanks a lot for your reply!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiago Braun wrote:It´s already set to Employee.setTopSalary(199000), is it not?

No. Line 20 is: e2.setTopSalary(199001);

There are different instances of the Employee class

The nature of static members is that they are shared among all instances. Each instance does not have its own copy of topSalary.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiago Braun wrote:In this case, I need to print e2 salary, wich is 199001.


I don't think that can be right. Your Employee class doesn't store the salary of any employees. It only stores the "top" salary among all Employees, in other words the highest value passed to the "setTopSalary" method.

However the questions you are supposed to answer don't say anything about salary at all, so trying to interpret the meaning of the variables is just getting in the way. Especially since the posted code is an extremely bad implementation of anything you might need for a real-life Employee. So just follow the instructions as given and don't try to interpret what you see there. Imagine they are in a foreign language which you don't understand at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!