Results 1 to 7 of 7

Thread: How to use a class in Java?

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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(
    0.0 && 20.0){
                    
    length l;
                }
    /// end if

            
    }// end setLength

            
    public double getWidth(){

                return 
    width;

            }
    //end getWidth

            
    public void setWidth(double w){

                if(
    0.0 && 20.0){
                    
    width w;
                }
    // end if

            
    }//end setWidth

            
    public double perimeter(){

                
    double thePerimeter 0;

                
    thePerimeter length 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 mainString 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.


  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: How to use a class in Java?

    Create an instance like this:
    Rectangle testIt = new Rectangle();

    That should resolve the cannot reference static.......

  3. #3
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: How to use a class in Java?

    Your 'Rectangle' class needs a constructor.

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  5. #5

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    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.


  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    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.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width