开发者

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 开发者_JAVA技巧me to access the name of the friend belonging to an instance of Person?

Also, more generally in Java can:

  • Local variables can be declared private?
  • Formal parameters may be declared final?
  • Does declaring an object automatically allocate space for it?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜