Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I am using java and starting to learn about objects. Is there a way to make an object public so that other methods can access the object? Thanks. ~Linux1245...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2006
    Posts
    55

    Java programming question--Objects


    Hello, I am using java and starting to learn about objects. Is there a way to make an object public so that other methods can access the object? Thanks.

    ~Linux1245

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    I'm not sure what you're asking. What are you trying to accomplish, specifically? Data sharing? Method sharing? If you're just trying to make data inside an object available to the outside world all you need is a public "getXXX" method that will return the value to whoever calls it.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  3. #3
    Just Joined!
    Join Date
    Jan 2006
    Posts
    55
    I am creating a student enrollment program and in my main method I have a switch statement with one option that takes you to a method addStudent() which lets you enroll as many students as you want. In the for loop is where it asks you what the name is and everything and also in the for loop is where I create the object newStudents and passes 5 arguments to the other class Students. I am trying to let the user view all the enrolled students when they select that option in the switch statement. When I try to call the object newStudent with all the information of that student, it says that newStudent could not be resolved, so I'm guessing it could not find that object. I tried to call the object's toString() and a get method but neither would work because it said that newStudents object could not be resolved. I'm guessing that since the newStudent's object was created in the for loop of a different method, that it is not open to any other method. Which is why I would like to make that object public so that any method in this class can call that object. Thanks again.

    ~Linux1245

  4. #4
    Linux User IsaacKuo's Avatar
    Join Date
    Feb 2005
    Location
    Baton Rouge, LA, USA
    Posts
    405
    It sounds like you've got a problem with scope. Any variable created within a loop will be destroyed and inaccessable once that loop ends. However, objects which are created within a loop can be "rescued" from death by assigning a more permanent variable to it. The most permanent variables you want are members of an object.

    It seems to me that you're not "getting" the basics of object oriented programming, and you really need a one-on-one discussion with a teacher or TA to get up to speed.

    Basically, your main program is an object. This object gets created when the program is run, and lives until the program exits. Any variables you declare within the main object at the topmost level are actually members of the object--they get created when the program is run and live until the program exits. Your list of students needs to be stored in a member of the main program object.
    Isaac Kuo, ICQ 29055726 or Yahoo mechdan

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2005
    Location
    Hell
    Posts
    514
    Quote Originally Posted by Linux1245
    I am creating a student enrollment program and in my main method I have a switch statement with one option that takes you to a method addStudent() which lets you enroll as many students as you want. In the for loop is where it asks you what the name is and everything and also in the for loop is where I create the object newStudents and passes 5 arguments to the other class Students. I am trying to let the user view all the enrolled students when they select that option in the switch statement. When I try to call the object newStudent with all the information of that student, it says that newStudent could not be resolved, so I'm guessing it could not find that object. I tried to call the object's toString() and a get method but neither would work because it said that newStudents object could not be resolved. I'm guessing that since the newStudent's object was created in the for loop of a different method, that it is not open to any other method. Which is why I would like to make that object public so that any method in this class can call that object. Thanks again.

    ~Linux1245
    Can you post your code? What is "newStudents"? What class is "addStudent()" part of? What class is your main() part of? Did you create an instance of the object before you try to use it?

Posting Permissions

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