I have class Employe that has variables like id , name ... and 2 other classes that inherit from Employe : Seller and Cashier. To calculate their salaries, I created a method in each one of Seller and Cashier but I need to access the name via the name getter method in Employe so I'd have :
System.out.println("The salary is "+Seller.getName() +" is : " +salary);
Once I type that, I get an error sayingI need to make the name variable to static, but I need it as non static since I'm creating multiple objects using the name variable. Any solution to this problem?
EDIT : This is the Employe class :
public class Employe {
protected int id;
protected String name;
protected String adresse;
protected int nbrHours;
public Employe () {
}
public Employe (int id, String name, String adresse, int nbHours)
{
this.id=id;
this.name=name;
this.adresse=adresse;
this.nbrHours=nbHours;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setNom(String name) {
this.name = name;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
}
This is the Seller class :
public class Seller extends Employe {
private int prime;
public Seller (int id, String name, String adresse, int nbHours,int prime)
{
super(id,name,adresse,nbHours);
this.prime=prime;
}
public int getPrime() {
return prime;
}
public void setPrime(int prime) {
this.prime = prime;
}
@Override
public String toString() {
return super.toString() +" [prime=" + prime + "]";
}
public void salaireSeller ()
{
int salaire = 0;
if(nbrHours<160)
{
salaire = nbrHours * 10 + prime;
System.out.println("La salaire de "+Responsable.getName() +" est : " +salaire);
}
else if(nbrHours >160)
{
int Extra= (160 - nbrHours) * 12;
int salaire1 = 160 * 10000 + prime;
salaire= Extra + salaire1;
System.out.println("La salaire de "+Seller.getName() +" est : " +salaire);
}
}
In the Main class I created a Seller object :
Seller Sel1 = new Seller(2, "Alex", "Miami", 18, 200);
now I want to calculat its salary using the SalaireSeller() method in the Main class of course :
Sel1.salaireSeller();
but in the Seller class :
System.out.println("La salaire de "+Responsable.getName() +" est : " +salaire);
it says I need to set Name to static, this will give every object the same name
-
Could you provide a fuller snippet? It's a bit hard to give a meaningful answer without somemore context.Mureinik– Mureinik2015年10月03日 23:47:20 +00:00Commented Oct 3, 2015 at 23:47
-
Change your modifier of the getter in the Employee class? Since Seller and Cashier are inherited... Otherwise post a minimal reproducible example with your code?t0mm13b– t0mm13b2015年10月03日 23:47:38 +00:00Commented Oct 3, 2015 at 23:47
-
Well, which seller's name do you want to get? Do you want to get the current employee's name, but only if it's a seller?Stack Exchange Broke The Law– Stack Exchange Broke The Law2015年10月03日 23:51:23 +00:00Commented Oct 3, 2015 at 23:51
-
The problem is that my code is in french, I don't know if you'll be able to get it but Ill post it.Hadh– Hadh2015年10月03日 23:53:17 +00:00Commented Oct 3, 2015 at 23:53
-
Why is the method in Seller in the first place? Is the employee name different because he's a Seller or a Cashier?user207421– user2074212015年10月04日 00:00:12 +00:00Commented Oct 4, 2015 at 0:00
3 Answers 3
You need a Seller instance, to call getName() and getSalary() on.
Seller s = new Seller();
// ...
System.out.println("The salary is " + s.getName() +
" is : " + s.getSalary());
Comments
You're certainly trying to access an instance variable from a static method.
What you want to do here is to create an instance of your class, then call the getName() method on the object created.
Seller sell = new Seller();
sell.setName("Jean-Paul"); // This is just an example
System.out.println("His name is " + sell.getName()); // Prints : His name is Jean-Paul
Comments
I figuered out a solution, I just need to add the name to the toString() method in class Employee, then add the salary variable to the toString() method in Seller class, without System.out.println(..) in SalaireSeller().
or instead of Seller.getName(), I use this.getName() and it works.