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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: accessing variables of constructors

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default accessing variables of constructors

    Basically I want to access variables of the constructor to do some computations and print out the variables of the constructor.


    class one
    {
    main()
    {
    something = method()

    <----------------------I want to display the variables from the constructor that were obtained from the method
    }
    method ()
    {
    }
    }

    class two

    //default constructor
    two
    {
    String variable 1 ="";
    String variable 2 ="";
    String variable 3 ="";
    }

    get() and set() for the variables in the constructor


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: accessing variables of constructors

    To access variables after construction, you can declare them global for instance. To print them, use System.out.println in appropriate places.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: accessing variables of constructors

    Quote Originally Posted by nikki101 View Post
    Basically I want to access variables of the constructor to do some computations and print out the variables of the constructor.
    Assuming class one creates an object of class two, and then attempts to get values from class two to print during the life of class one? I take "variables of the constructor" to mean "variables declared in the constructor" , by looking at the code for class two:
    Quote Originally Posted by nikki101 View Post
    class two
     
    //default constructor
    two
    {
    String variable 1 ="";
    String variable 2 ="";
    String variable 3 ="";
    }
     
    get() and set() for the variables in the constructor
    It is not possible like that. The scope of those variables is gone once the constructor returns, and the constructor does not return values. Either use those variables in a larger scope (class or instance variables) or the constructor will have to send those values out during construction through method calls.

    On the same token, get() and set() would have no access to the values of variables declared in the constructor. So I am confused, did you mean to declare those Strings as instance variables and initialize them in the constructor? The pseudo code does not quite fill in all of the blanks.

    Can you explain more what you are trying to do? ..There are ways to do what you want to do, the best way depends on exactly what you are trying to do.

Similar Threads

  1. problem accessing variables in multiple classes
    By javaiscool in forum Java Theory & Questions
    Replies: 4
    Last Post: March 20th, 2013, 06:15 PM
  2. Accessing Variables Between Classes
    By thudgun in forum Object Oriented Programming
    Replies: 9
    Last Post: January 2nd, 2012, 01:08 AM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  5. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 11:50 AM