Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 10:25 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
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:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 10:27 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
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.
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. } }
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 10:29 AM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
That is demo program: (My program is MyApplet.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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get next available number from MS access shahjehan Database 3 11-18-2007 09:41 PM
Access with MS SQL cachi Database 1 08-07-2007 09:54 AM
Help with access a database using Microsoft Access cachi Database 1 08-07-2007 09:51 AM
variable access from another file riadmazloum AWT / Swing 1 08-06-2007 09:13 PM
access cd drive milinkp New To Java 0 07-20-2007 08:14 AM


All times are GMT +3. The time now is 11:45 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org