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.

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.

Sunday, November 21, 2010

Java - Example - mechanism to note: method in a Class calling another method in the same Class

mechanism to note: method in a Class calling another method in the same Class

Java - Example - A Class with an Instance Variable, a set Method and a get Method

In this example, our class maintains the course name as an instance variable, so that it can be used or modified at any time during our application's execution.

Our class contains 3 methods - setCourseName, getCourseName, and displayMessage.

The method setCourseName stores a course name in a GradeBook (on object instance of our class)

The method getCourseName obtains a GradeBook's course name.

The method displayMessage, which now has no parameters being passed into it, still displays a welcome message, but also includes the course name of that object in the welcome message => what you should note is, displayMessage now obtains the course name be calling another method in the same class [ the getCourseName method] !!


Reference: "Java How to Program, by Deitel & Deitel"

Saturday, November 20, 2010

Java - Instance Variables (2)

Classes normally have methods that manipulate the attributes that belong to object instantiations of the class.

Attributes are realized in the form of variables inside a class declaration. These attributes(variables) are declared inside the class declaration but outside the bodies of the class's methods.

**When each object of a class maintains its own copy of an attribute, the field that represents the attribute is also known as an "instance variable" - each object (instance) of the class has a separate instance of the variable in memory.


Reference: "Java How to Program, by Deitel & Deitel"

Java - Instance Variables

Prior to this point, all the the variables we used we declared in an application's main method.

When a variable is declared in the body of a method, it is categorized as a "local variable', and that variable can only be used inside that method.

When the particular method terminates (right after it finishes execution), all values of its local variables terminate with it.

**On the other hand, objects can have attributes that it carries with it, regardless of whether one of its methods are in execution or have finished execution or not.

**These attributes exist before a method is called (as soon as an instance of the object is instantiated), and continues to exist independent of the execution or completion of any of its methods.

Friday, November 19, 2010

Java - Creating a GradeBook Object and passing a String to its displayMessage method

import java.util.Scanner;

public class GradeBookTest
{
      public static void main(String[] args)
      {
          Scanner myScanner = new Scanner(System.in);

          GradeBook myGradeBook = new GradeBook();

          System.out.println("HL: Please enter your course name:  ");

          String nameOfCourse = myScanner.nextLine();

          myGradeBook.displayMessage(nameOfCourse);
      } //end main
} //end GradeBookTest class



Reference: "Java How To Program, by Deitel & Deitel"

Java - Class Declaration with a Method that has a Parameter

public class GradeBook
{
      public void displayMessage(String courseName)
      {
         System.out.printf("Howard welcomes you to the gradebook for %s \n", courseName);
      } //end displayMessage method
} //end GradeBook class




Reference: "Java How to Program"