javaoop

Basic Java OOP Questions on variable declaration and access


If I have a class Person which has two instance variables: name of type String and friend of type Person, how can I add a method to Person that will allow me to access the name of the friend belonging to an instance of Person?

Also, more generally in Java can:


Solution

  • Question 1:
    Honestly, this is the most basic practical object-oriented question out there. If you don't understand it, you need to go talk to someone.

    True or False: Local variables may be declared private.
    Local variables can never be accessed by outside code; it makes no sense to declare them private.

    True or False: Formal parameters may be declared to be final.
    True - it means that the method can't reassign the parameter locally.
    /* Edited according to the comment below */

    True or False: Declaring an object (for example, Person p;) allocates space for that object
    Objects in java are all references. When you declare an object, it allocates a reference which doesn't refer to anything in particular. When you later allocate the object, it then sets that reference to point to an allocated section of memory in which the object is stored.