Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to call the homeValue method from the house class to the method valOfHouse in the HouseHelper class. I keep getting an error "non static method cannot be referenced from a static content". How can i fix this code? I can only make changes to the HouseHelper class.

Java
public class House 
{ 
public House (String _location, String _realtor, String _color, int _rooms, int _price, int _size, int _year) 
{ 
this.location = _location; 
this.realtor = _realtor; 
this.color = _color; 
this.rooms = _rooms; 
this.price = _price; 
this.size = _size; 
this.year = _year; 
} 
public int homeValue() 
{ 
Calendar calendar= Calendar.getInstance(); 
int ageOfHome = calendar.get(Calendar.YEAR) - year; 
if (ageOfHome <= 0) 
ageOfHome=1; 
int older = ageOfHome * 12000; 
int new = ageOfHome * 10000; 
int val = price; 
if (price > older) 
currentVal -= age * 1550; 
else if (price < new) 
val -= ageOfHome * 1300; 
else 
val -= ageOfHome * 1400; 
return val; 
} 

public class HouseHelper 
{ 


 public int valOfHouse() 
{ int total = 0;
  total = House.homeValue();

 return total; // non static method cannot be referenced from a static content
 }


What I have tried:

public class HouseHelper
{


public int valOfHouse()
{ int total = 0;
total = House.homeValue();

return total; // non static method cannot be referenced from a static content
}
Posted
Updated 17-Apr-16 21:36pm
Comments
Mohibur Rashid 17-Apr-16 21:56pm    
Is this your actual code? In your constructor "House" you have used many variable that start with "this." But I don't see the declaration.
I can see you have defined an integer type variable name 'new'

Have you put any thought before you write that code?
You have missed all the basics.
Sergey Alexandrovich Kryukov 17-Apr-16 22:00pm    
It just should not be fixed. You don't need a "fix", you only need understanding. What is static method and what is instance method, and same thing about all other members. Note that without understanding of this basic concepts, you can say bye-bye to Java and all other OOP technologies. And when you lean and understand it, you will simply not run in such problems, ever. You just won't write the kind of code you have now.
—SA

I suggest you go to The Java Tutorials[^] and work through the study guides.
 
Share this answer
 
As the error message said, preceding a method (homeValue()) with a class name (House) only works if homeValue() is a static method, i.e.
Java
public static int homeValue()

otherwise, you have to instantiate the House class first, i.e.
C#
House house = new House();
int total = house.homeValue();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900