Simple Java OOP Question

Discussion in 'OT Technology' started by doggfather, Apr 12, 2006.

  1. doggfather

    doggfather New Member

    Joined:
    Aug 6, 2005
    Messages:
    224
    Likes Received:
    0
    public class Shape {
    public static void main(String[] args) {
    Rectangle sq = new Rectangle(5,6);
    System.out.println(sq.Area());
    }
    class Rectangle {
    double x;
    double y;

    double Rectangle(double vX,double vY) {
    x = vX;
    y = vY;
    }

    double Area() {
    return x*y;
    }
    }

    }

    I keep getting:

    Swing.java:3: non-static variable this cannot be referenced from a static context
    Rectangle sq = new Rectangle(5,6);
    1 error

    Could someone help me?
     
  2. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    :ugh: I used to get that all the time. Give me a bit to try to remember what's causing it.
     
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    :uh: Your constructor can't return a value. Change

    double Rectangle(double vX,double vY) {
    x = vX;
    y = vY;
    }

    to

    Rectangle(double vX,double vY) {
    x = vX;
    y = vY;
    }

    The compiler thinks you're calling a non-static member function (with no object instantiated), instead of constructing an object - because constructors can't have a return value.
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    I've made that same mistake dozens of times. :mamoru:
     
  5. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Really? I don't understand why anybody would fall into this particular trap. The return value of a class constructor is an instantiated object. (so to speak.) Why would you even make a class if all you want is a function that immediately returns a value, anyway?

    I'm not trolling, I'm just curious what's causing the disconnect between expectation and reality here.
     
  6. doggfather

    doggfather New Member

    Joined:
    Aug 6, 2005
    Messages:
    224
    Likes Received:
    0
    it still doesnt work once I change it,

    it says like this is not a constant value or something...

    on this line:
    Rectangle sq = new Rectangle(5,6);

    could someone try compiling it and try to fix it, shouldn't be hard to debug but I'm new to Java thats why.
     
  7. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    There's no disconnect, and it's not a trap. It's habit to write functions with a return value. So often without thinking about it, you might include a return type with the definition of a constructor.

    Defining your constructor with a return type isn't necessarily misunderstanding the language, in the same way that typing "becuase" doesn't necessarily mean you can't spell.
     
    Last edited: Apr 13, 2006
  8. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    While I wait for the jdk to download, do you happen to have the exact error message?
     
  9. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Is that code in one file? Java only likes one public class per file. Try placing the rectangle class in a seperate file.
     
  10. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    Java isn't my first language and it's been a while since I've worked much with it, so there's probably a better answer than this.

    I don't know if it's legal to have a class definition inside another class definition. I was able to get it working by pulling class Rectangle out into a separate file and importing it.
     
  11. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    See? I was right. :mamoru:
     
  12. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    If Java is similar to C++ in this particular way, then you can't create a pointer to an object AND assign it an object to point to in the same line of code. Try "Rectangle sq; sq = new Rectangle(5, 6);" and see if that works. If not, I guess I remember less than I think I do.
     
  13. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I can't say I sympathise. My personal approach is "void until proven otherwise", so it's just never occurred to me to try to make a constructor return a value. I suppose it's "bad programming" to write functions that doesn't return status values at least, but this has yet to hamper my codewriting in real life.
     
  14. an0nymous

    an0nymous Anon E. Mouse

    Joined:
    Jan 7, 2005
    Messages:
    710
    Likes Received:
    0
    Rectangle sq = new Rectangle(2,3); is legal in java
     
  15. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I see. Nevermind, then.
     
  16. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    Rectangle *sq = new Rectangle(2,3); would be legal in C++ too. There is no restriction that says you can't declare a pointer and assign it a variable at the same time in C++. If you wanted to do this without a pointer in C++, you just need to write the line like this: Rectangle sq(2,3); Since every object is a pointer in Java, there is no real equivalent for the second C++ method in Java.
     
  17. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    Java allows named inner classes and anonymous inner classes. What he has is correct for defining an inner class (once the constructor return type is removed).

    Thread starter: Is your class really named Swing.java with the class Shape? The class name and file name are supposed to match. Also, what is the current error message you are receiving? We can diagnose the problem a lot easier with the proper error message.
     
  18. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    Code:
    public class Shape {
       public static void main(String[] args) {
          Rectangle sq = new Rectangle(5,6);
          System.out.println(sq.Area());
       }
    
       class Rectangle {
          double x;
          double y;
    
          double Rectangle(double vX,double vY) { <-- [b]WRONG[/b]
          Rectangle (double vX, double vY) { <-- [b]RIGHT[/b]
             x = vX;
             y = vY;
          }
    
          double Area() {
             return x*y;
          }
       }
    }
    the reason you're getting that error is because you're calling a non-static class (Rectangle) with a static context. You're calling a method without instantiating the class first, kind of like telling a dog to sit when you don't have a dog.

    Try this:
    Code:
    public class Shape {
    
       public static void main(String[] args) {
          Shape test = new Shape() {
          test.run();
       }
    
       class Rectangle {
          double x;
          double y;
    
          Rectangle (double vX, double vY) {
             x = vX;
             y = vY;
          }
    
          double Area() {
             return x*y;
          }
       }
    
       void run() {
          Rectangle sq = new Rectangle(5,6);
          System.out.println(sq.Area());
       }
    }
     
    Last edited: Apr 14, 2006
  19. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,395
    Likes Received:
    581
    It's legal in C++ as well, as long as you make it a pointer to Rectangle:

    Rectangle *sq = new Rectangle(2,3);

    Nevermind, I see I'm late. :o
     

Share This Page