-1

If I create an object with a constructor in one class, and the constructor gives the object a property like 'name', is there a way to access specifically the String 'name' from another class? As in, if I have a method that I pass the objects to, and that method needs to access just the String 'name' from the constructor, how do I get to it?

This is probably a bad question that already has an answer, but since I don't know the right terminology to search for it I'm a bit stuck...

user13948
  • 443
  • 1
  • 6
  • 14
  • You can add a getter to your class so that consuming code may access that value within the object. This really has nothing to do with constructors. – David Dec 12 '15 at 22:36
  • Yeah, like @David was saying. Do you know about getter and setter methods? (I'm also not being a smart ass btw). – Mike Dec 12 '15 at 22:38
  • @David OK, so there's no way to get to it just by passing the object to the method? – user13948 Dec 12 '15 at 22:38
  • @Mike Not actually 100% sure, glad you asked! – user13948 Dec 12 '15 at 22:39
  • @Karacoreable: Well, no. The mere act of passing an object to a method doesn't itself extract information from the object. The code within that method can access any methods on the object, though. So if there's a method which returns that value then that code can invoke that method. Not to sound unhelpful, but this is generally covered by pretty much any introductory Java tutorial. Have you tried something specific that didn't work? – David Dec 12 '15 at 22:39
  • 1
    Possible duplicate of [How do getters and setters work?](http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – Mike Dec 12 '15 at 22:39
  • @Mike Replies there seem to be saying I shouldn't be using getters and setters... although the argument and the alternatives are far too complicated for me! – user13948 Dec 12 '15 at 22:45
  • @Karacoreable: If you have a value in your object that other code needs to access, the simple and direct solution is to add a getter. As to whether or not you *should* do this or whether or not there's a *better* design for your application, nobody here can know based on the information given. – David Dec 12 '15 at 22:48
  • @Karacoreable There are lots of opinions on getter and setter methods and their usefulness in programming. It really depends on exactly the context within which you are accessing the instance variables. I'm having difficulties discerning exactly what you saying in your question. I have a feeling, like Dave probably does, that what you want is a simple getter method. – Mike Dec 12 '15 at 22:48
  • @Mike (and David) Thanks. It probably is what I want, or I'd be able to understand the alternatives! – user13948 Dec 12 '15 at 22:50

2 Answers2

1

You cannot read parameters passed to a constructor from outside the constructor definition, unless that parameter is stored in a field of the class. Of course, if you create an instance of a class like MyClass myObject = new MyClass("Some String");, you can access some string in the scope of the code that created the object.

The ways that a field of a class, say the field fieldName from an instance myObject an a class MyClass can be accessed by another class are:

  • If the field is public, access by myObject.fieldName
  • If the field is protected, access it by subclassing MyClass
  • If MyClass has a getter for the field: myObject.getFieldName()
  • If the field is private and does not have a getFieldName() method, then it cannot be accessed from outside of the class.
aaroncarsonart
  • 1,054
  • 11
  • 27
0

Here are a couple of classes that I think demonstrate what you are wanting to do. I have a Person class that has a name field and a Friend class that has a method called sayHello and it accesses the name property of Person.

public class Main
{
    private static class Friend
    {
        public void sayHello(Person person)
        {
            System.out.println("Hello " + person.getName());
        }
    }

    private static class Person
    {
        private String name;

        public Person(String name)
        {
            this.name = name;
        }

        public String getName()
        {
            return name;
        }
    }   

    public static void main(String[] args)
    {
        Person person = new Person("John Smith");
        Friend friend = new Friend();
        friend.sayHello(person);
    }
}
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37