How to use a class in Java?
Hey guys I have created a class which compiles fine, but I am having trouble trying to use it in a program. I am not clear on how to instantiate the class in a program can you give me an example of how to do this?
Thanks!
Here are my class and program so far.
The Class....................................................
PHP Code:
public class Rectangle{
private double length = 1;
private double width = 1;
public double getLength(){
return length;
}//end getLength
public void setLength(double l){
if(l > 0.0 && l < 20.0){
length = l;
}/// end if
}// end setLength
public double getWidth(){
return width;
}//end getWidth
public void setWidth(double w){
if(w > 0.0 && w < 20.0){
width = w;
}// end if
}//end setWidth
public double perimeter(){
double thePerimeter = 0;
thePerimeter = length * 2 + width * 2;
return thePerimeter;
}//end perimeter
public double area(){
double theArea = 0;
theArea = length * width;
return theArea;
}//end perimeter
}//end class Rectangle
The program to use it...........................
PHP Code:
import javax.swing.JOptionPane;
public class RectangleTest{
private Rectangle testIt;
public static void main( String Args[] ){
testIt = new Rectangle();
testIt.setLength(5.0);
testIt.setWidth(6.0);
JOptionPane.showMessageDialog(null, "The rectangle's width perimeter is: "
+ testIt.perimeter + "\nThe rectangle's area is: "
+ testIt.area, "Results", JOptionPane.PLAIN_MESSAGE);
}//end main
}//end class Rectangle
These are the errors I get
C:\...\RectangleTest.java:12: non-static variable testIt cannot be referenced from a static context
testIt = new Rectangle();
^
C:\...\RectangleTest.java:14: non-static variable testIt cannot be referenced from a static context
testIt.setLength(5.0);
^
C:\...\RectangleTest.java:15: non-static variable testIt cannot be referenced from a static context
testIt.setWidth(6.0);
^
C:\...\RectangleTest.java:18: non-static variable testIt cannot be referenced from a static context
+ testIt.perimeter + "\nThe rectangle's area is: "
^
C:\...\RectangleTest.java:18: cannot find symbol
symbol : variable perimeter
location: class Rectangle
+ testIt.perimeter + "\nThe rectangle's area is: "
^
C:\...\RectangleTest.java:19: non-static variable testIt cannot be referenced from a static context
+ testIt.area, "Results", JOptionPane.PLAIN_MESSAGE);
^
C:\...\RectangleTest.java:19: cannot find symbol
symbol : variable area
location: class Rectangle
+ testIt.area, "Results", JOptionPane.PLAIN_MESSAGE);
I'm not sure what the errors mean or what I am doing wrong... :eek:
Re: How to use a class in Java?
Create an instance like this:
Rectangle testIt = new Rectangle();
That should resolve the cannot reference static.......
Re: How to use a class in Java?
Your 'Rectangle' class needs a constructor.
Re: How to use a class in Java?
Quote:
Originally Posted by x-ice
Your 'Rectangle' class needs a constructor.
That's true, but default constructors are automatically provided. If you specify a default constructor then the compiler does not provide one for you. Either way you should always write them in for good coding practice.
Re: How to use a class in Java?
Yah I thought the global decleration private Rectangle testIt; covered it, but I guess not. I just changed it to wha tyou suggested and it worked fine.
Thanks!
Re: How to use a class in Java?
Quote:
Originally Posted by Arc
Yah I thought the global decleration private Rectangle testIt; covered it, but I guess not.
That's not a global declaration. It's a class member variable (field in Java terminology), which only exists within an instance of the class. In the static method main(), you have no instance.
Re: How to use a class in Java?
Just to elaborate on CB's comment. The main() method is a static Class method. A static classs method or variable does not require a real instance of that object to exist for it to be invoked or referenced.
Anything which is not declared as static in the class definition does not exists until an instance of that object has been created. Then, it only exists for that particular instance.
In short, to solve your problem, you need to declare testit as static:
Code:
private static Rectangle testIt;
You can never reference or invoke any non static members of a class without an object instance. However, you can invoke static methods and reference static properties of a class from an instance of that object.