Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default Access a variable

    I have a MyApplet class. In it, I declare 1 instance variables is: x = 13
    and I have a button. I want then I clicked this button, a frame showed.

    My frame have 1 textfield and 1 button. I want then I clicked this button, value of variable x show in textfield.

    I tried created a method as getx() in MyApplet class:
    Java Code:
    public double getx() {
       return x;
    }
    Then at MyFrame class, I tried to use x , but program show 1 error ( cannot find symbol x).
    - None stop, I tried to declare a new object have type MyApplet
    (MyApplet obj = new MyApplet) then use obj.getx() to take value of x variable. But result returned is 0.0

    How do I must to take value of x variable from MyApplet class ?

    Thanks
    Eric

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    We would need to see your whole code in order to tell you. My guess is that x isn't within the scope of your class where you want to return it.
    Java Code:
    public class test
    {
    
      public test ()
      {
        int x = 1;
      }
    
      public int getX()
      {
        return x;  // WILL NOT WORK.  We can't see x here because it was declared in the method constructor.
      }
    }
    Java Code:
    public class test
    {
      private int x;
      public test ()
      {
        x = 1;
      }
    
      public int getX()
      {
        return x;  // WILL WORK.  x is now in the scope of the class and so methods within that class can use it.
      }
    }
    Greetings.
    Felissa:p

  3. #3
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default

    That is demo program: (My program is MyApplet.java)

    Java Code:
    public class MyApplet extends Applet
    {
        public double x1;
        
        public MyApplet() {
             x1 = 30;
        }
    
        public double getX() {
             return x1;
        }
    
       //(at here have a button which then clicked will show MyFrame)
       public void actionPerformed(ActionEvent ae)
       {
           if (ae.getSource().equals(btnShow)) {
                MyFrame oFrame = new MyFrame();
                oFrame.setVisible(true);
           }
       }
    }
    
    class MyFrame extends Frame
    {
        public double x2;
        MyFrame()
        {
              x2 = x1 * 20;
              System.out.print("Value is : " + x2);
        }
    }
    I run program above, it have a error ... (cannot find symbol x1)
    If I declared ( MyApplet obj = new MyApplet() ) then use obj.getX() to take value of x1, compile successfull but then I run it's show result x1 = 0.0. Why?

    Thanks
    Eric

Similar Threads

  1. Replies: 3
    Last Post: 11-18-2007, 08:41 PM
  2. Access with MS SQL
    By cachi in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 08:54 AM
  3. Replies: 1
    Last Post: 08-07-2007, 08:51 AM
  4. variable access from another file
    By riadmazloum in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:13 PM
  5. access cd drive
    By milinkp in forum New To Java
    Replies: 0
    Last Post: 07-20-2007, 07:14 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
  •