Monday, November 22, 2010

Java - Classes are type specifiers

Classes are type specifiers that specify the types of objects. (int, char, Boolean, float, etc., are among other type specifiers)

Java – fields of an object (instance variables, as opposed to local variables ) have default initial values in Java

Note that if you directly print out the value of a declared but uninitialized field that was declared as of type String, you would get “null” displayed on the screen instead of junk. Unlike local variables, which are not automatically initialized, every field has a default initial value – a value specified by Java when you do not initialize the field value.

**The default value for a field of type String is null.

Java - output a blank line

Output a blank line in Java:

System.out.println();

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.