Monday, November 22, 2010

Java - Where to declare your instance variables

In theory, in Java you can list your class’s fields anywhere in the class outside of the class’s method declarations, but scattering tends to result in code that is hard to read, therefore, we like to list a class’s fields first.

Java - General Rules of Thumb


In general, instance variables should usually be declared private, and methods should usually be declared public (there are times when it is also appropriate to declare methods private, when you only what other methods in the same class to be able to access the method)

Java - Data Hiding / Encapsulation

The practice of declaring instance variables using the private access modifier is called data hiding (encapsulation). => can only be accessed by methods of the same object’s class.  => this prevents the object’s instance variable from being accidentally changed by some other class in the program.

Java - Data Hiding / Encapsulation

The practice of declaring instance variables using the private access modifier is called data hiding (encapsulation). => can only be accessed by methods of the same object’s class.  => this prevents the object’s instance variable from being accidentally changed by some other class in the program.

Java - Access Modifiers public and private

Most instance variables are declared to be private.  This makes them accessible only to the methods in the class from which they are declared.

Java – Instance variable

All methods of the class can manipulate any instance variables that appear in the class.

Java - What is the difference between String and String[] ?

String is just one string.

String is an array of String's.