10/02/2008

Class

The structure of Java Class

example)

package learn.java.sample;
import java.util.*;

public class SampleClass {

    String str = "Hello";    // String variable
    int x = 100;    // primitive

    /* main method */
    public static void main(String[] args) {
        SampleClass sc = new SampleClass();
        sc.showMsg();
        System.out.println(getValue());
    }
    
    /* method */
    void showMsg() {
        System.out.println(str);
    }

    /* method */
    int getValue() {
        return x;
    }
}
-----

How to make a class (Basic)
  • Declare the name of the package
  • Import necessary packages
  • Define the name of the class
  • Declare instance variables (and initialize them if you want)]
  • Write main method 
What is main method?
main mehtod is the only method where the program thead starts.