Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using values in different classes

  1. #1
    Join Date
    Feb 2010
    Thanked 1 Time in 1 Post

    Default Using values in different classes

    Okay, I'm wondering if it's possible to use a value from one class in another class. Specifically, a JTextField value. I think I know how to do it normally:

    First class:
    public String textFieldValue()
    return x;
    Second class:

    But in my code, a button must first be pressed (using an action listener) to get the JTextField values. This means that I have to use this:

    public void actionPerformed(ActionEvent e) {
    //get JTextField values

    As you probably know, you can't return any values within a void method so I have no idea how to get these values to another class.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Using values in different classes

    I'm a bit unclear on the question, so perhaps more code is in order. If you follow encapsulation guidelines, variables will be private and accessed via getters and setters
    //Demo class with a single private variable
    public class A{
        private int val = 0;
        public int getVal(){
            return val;
    //Demo class which accesses A
    public class B{
        A a = new A();
        public void function(){
            int val = a.getVal();
            //do something with val

  3. #3
    Join Date
    Feb 2010
    Thanked 1 Time in 1 Post

    Default Re: Using values in different classes

    Hi Copeg, I believe you helped me with my other problem The code you showed I understand, however my problem is a bit different. I want to get a variable (which happens to be under a void method, so I can't return its value) to be accessible by another class. I don't know how to do this for the exact reason I mentioned in the parenthesis. Putting "public" in front of the variable doesn't do anything because the value for that variable changes after I run the class.
    Last edited by The_Mexican; November 19th, 2010 at 08:08 PM. Reason: Grammer

Similar Threads

  1. Need help passing values through classes
    By Jared in forum Java Theory & Questions
    Replies: 2
    Last Post: October 6th, 2010, 08:42 AM
  2. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  3. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  4. How to share variable values amongst different classes?
    By igniteflow in forum Object Oriented Programming
    Replies: 8
    Last Post: August 20th, 2009, 08:53 AM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM