|
-
Oct 27th, 2005, 12:56 AM
#1
Thread Starter
PowerPoster
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...
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Oct 27th, 2005, 05:08 AM
#2
Frenzied Member
Re: How to use a class in Java?
Create an instance like this:
Rectangle testIt = new Rectangle();
That should resolve the cannot reference static.......
-
Oct 27th, 2005, 05:08 AM
#3
Fanatic Member
Re: How to use a class in Java?
Your 'Rectangle' class needs a constructor.
-
Oct 27th, 2005, 05:21 AM
#4
Frenzied Member
Re: How to use a class in Java?
 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.
-
Oct 27th, 2005, 12:11 PM
#5
Thread Starter
PowerPoster
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!
-We have enough youth. How about a fountain of "Smart"?
-If you can read this, thank a teacher....and since it's in English, thank a soldier.

-
Oct 28th, 2005, 05:21 AM
#6
Re: How to use a class in Java?
 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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2005, 05:51 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|