Results 1 to 8 of 8
  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default First Time problem!

    Ok, Ive built some overly complex programs, and lately ive been trying to OOP more, making things neater, but ive run into my first OOP problem! Ive made a demonstration program, here it is!

    Java Objects:
    Java Code:
    [B]Main.java[/B]
    [CODE]
    package vtest1;
    class Main{
        public static void main(String[] args) {
           Panel2 p2 = new Panel2();
           p2.setup();
        }
    }
    Panel1.java
    Java Code:
    package vtest1;
    public class Panel1 {
        public void intTest() {
            Panel2 p2 = new Panel2();
            int x = p2.x;
            int y = p2.y;
            [COLOR="Blue"]System.out.println(x+","+y);[/COLOR]
        }
    }
    Panel2.java
    Java Code:
    package vtest1;
    public class Panel2 {
        int x;
        int y;
        public void setup(){
            Panel1 p1 = new Panel1();
            x = 10;
            y = 10;
            print();
            p1.intTest();
        }
        public void print(){
            [COLOR="Red"]System.out.println(x+","+y);[/COLOR]
        }
    }
    [/CODE]

    The first output (Highlighted in red) prints the x and y, so it prints 10,10.
    The second output (Highlighted in blue) SHOULD print the x,y (10,10) also! but it doesn't, instead it prints 0,0.

    My problem is that my x,y veritable values are not being recognized in my panel1 object!? please help!

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Because the instance of Panel2 (p2) in intTest() is another one than the p2 in your main method! In initTest() you instantiate a new object and invoke only the default constructor which doesnt set the x and y variables, so they have their default values (0)
    Last edited by eRaaaa; 11-16-2010 at 05:47 AM.

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    So, how do i fix this?

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You need to somehow pass a reference of the panel2 object to the panel1 object.

    The simplest way is to modify the intTest method to
    Java Code:
    public class Panel1 {
        public void intTest(Panel2 p2) {
            int x = p2.x;
            int y = p2.y;
            System.out.println(x+","+y);
        }
    }
    you should then call this method like
    Java Code:
    p1.intTest(this);
    if you dont understand why, please ask.

  5. #5
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Haha, i understand the first code black modification, Its the p2 reference/deceleration. The second codeblack on the other hand, the "p1.intTest(this);" i do not understand!

  6. #6
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Quote Originally Posted by Jcbconway View Post
    Ok, Ive built some overly complex programs, and lately ive been trying to OOP more, making things neater...
    To be frank, your OOP example is quite complex too... Especially using two class that have almost the same name but different functionality and using two member variables with the same name (p2) doesn't help to understand the code easily.

    What exactly are you trying to demonstrate with this code?

    Erik
    I'm new to Java but I like to help where ever I can. :)

  7. #7
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Well, the demonstration is to just show my variable reference problem.

  8. #8
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

Similar Threads

  1. Time Zone Problem on Windows
    By yzaltz in forum New To Java
    Replies: 4
    Last Post: 09-29-2010, 01:38 PM
  2. Time Calculator Problem
    By ktisallred in forum New To Java
    Replies: 3
    Last Post: 09-22-2009, 07:15 PM
  3. System time problem
    By dswastik in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-08-2009, 01:36 PM
  4. Date n Time Display problem
    By kapilverma32 in forum Advanced Java
    Replies: 6
    Last Post: 02-10-2009, 01:53 PM
  5. Time-Date problem
    By teo.danciu in forum New To Java
    Replies: 5
    Last Post: 08-27-2008, 10:01 AM

Posting Permissions

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