Java Class

Java Class

  • A Java program contains a collection of classes, which are written by you and other classes provided by the Java language technology tools.
  • Nearly everything in Java is an object and known as instance of a Java class.
  • The idea is to create many objects out of the same Java class.
  • A class defines the capabilities for the created object, and every piece of program codes in a Java program must be contained inside a class.
  • You must specify that class for the Java runtime system which start with a call to that main() method.
  • Each public class must be defined in a file with the same name as the class name. The file must have the extension ".java".
    Example java file: car.java:
    // Displays "I am a car!" to the standard output
    public class car {
      public static void main(String[] args) {
        System.out.println("I am a car!");
      }
    }
    
  • All classes can contain members of different types and the most important are:
    1. Constructors (kind of method used to initialize the object status and has no return value),
    2. Instance variables (store information about the object state),
    3. Methods (used to perform actions or operations related to the class or object of the class),
    4. Static variables (state/information related to the class only)
    5. Static methods (operations or actions related to the class only and not to a particular object of the class).
    Example java file: car.java with all types of members
    
    public class Car {
    
      int _age;                       // instance variable
      static String _info = "I am made of steel!";  // static variable
    
      static String wheels() {        // static method
        return "I have 4 wheels!";
      }
    
      Car(int age) {                  // constuctor
        _age = age;
      }
    
      int getAge() {                  // method
        return _age;
      }
    
      public static void main(String[] args) { // static method
        Car myCar = new Car(2);        // instantiated object of class car
        System.out.println(myCar.wheels());
        System.out.println(myCar._info);
        System.out.println("My age is: " + myCar.getAge() + " year!");
      }
    }
    
    The result of this is:
    I have 4 wheels!
    I am made of steel!
    My age is: 2 year!
    You can download this example here (needed tools can be found in the right menu on this page).

Creating a class.

  • As a general rule in the design, you should keep the members that contain data (instance variables) in a class entirely private.
  • To access private member's data in a class, you should create public methods known as accessor methods.
  • A public accessor method in a class is used either to get a value from a private member variable or to set its value.
  • This is named java Encapsulation, which is the number one of the three main OOP Concepts. The meaning is to give the programmer full control of the actions the class can perform and control how to change the member variables..
    A class Cat example:
    // Cat class declaration
    // Data members are private, 
    // public accessor methods
    // mediate setting and getting 
    // the values of the private data
    public class Cat {
    
      private int age;
      private String name;
      // public accessors
      public int getAge() {
        return age;
      }
      public void setAge(int Age) {
        age = Age;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String Name) {
        name = Name;
      }
    
      // public member method
      public void Meow() {
        System.out.println("Meow - i am "+name);
      }
    
      public static void main(String[] args) { // static method
        Cat Frisky = new Cat();
    // set Frisky's age 
    // using the public accessors
        Frisky.setAge(5);
        Frisky.setName("Frisky");
    // use Frisky's other method
        Frisky.Meow();
        
      }
    }
    The result of this is:
    Meow - i am Frisky
    You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.