The java main() method and an Object creation process (#4)


----------Object creation by JVM-----------

Description:

The object car1 is an instance of Car class created by JVM.As shown below the new keyword is use to create car1 object from the Car class.

Car car1 = new Car();

Car class Reference -> http://namastejava.blogspot.jp/2015/01/java-source-translation-to-class-file-3.html

Each object is isolated from each other and maintains their own state (value for each property). e.g. car1 object’s price = 1000000, where as car2 object’s price = 2000.

-----------Source [CarApp.java]------------
public class CarApp {

public static void main(String[] args) {
//car1 instance
Car car1 = new Car();
car1.setPrice(1000000);
car1.setCurrency("JPY");
double p1 = car1.getPrice();
String c1 = car1.getCurrency();
System.out.println("Price1 = " + p1);
System.out.println("Currency1 = " + c1);
//car2 instance
Car car2 = new Car();
car2.setPrice(2000);
car2.setCurrency("USD");

//car3 instance
Car car3 = new Car();
car3.setPrice(5000);
car3.setCurrency("EUR");
}

}

Description:

The main() method
  •  is an entry point to the Application
  •  is a special method, which is already known to JVM
  •  should exist for each Java application
  •  is static, hence JVM can access it without Instance of the target class
  •  returns void
  •  takes parameters of type String array, these are use to pass values at the time of application start up.

Java source translation to the Class File (#3)

-----------1. Create “Source File” [Car.java] ---------

public class Car {

private double price;
private String currency;
public String getCurrency() {
return currency;
}
public void setCurrency(String c) {
currency = c;
}
public double getPrice() {
return price;
}
public void setPrice(double p) {
price = p;
}
}

Description:
Use “Eclipse” or any other editor to create source file, On Unix like OS “vi editor can be used.


-----------2. Do source [Car.java] compilation-------

On Windows: C:/>javac Car.java
On Unix like:  $javac Car.java


Description:
Compile the “Source File” to translate it into the “Class file” (byte code). Use “javac” command to compile the source. 

Syntax: javac <SOURCE_FILE> 


-----------3. Check for the class file [Car.class]-----

Car.class

Description:
Java compiler translate Car.java source file into Car.class. This class file will be use by JVM to create instance of class as necessary.

TYPES in Java (Data Types) (#2)

Java data types can be categorised into "Fundamental type" and "Reference type".

-----------------Fundamental types----------------
  • byte   (8 bit, -128 to 127)
  • short  (16 bit, -32768 and 32767)
  • int    (32 bit)
  • long   (64 bit)
  • float  (32 bit)
  • double (64 bit)
  • char   (16 bit, 0 to 65535)
  • boolean(1 bit, true or false)

Description:
Fundamental types are like LEGO blocks, using which you can create VARIABLES in your program.

VARIABLES are use to store value (state) of the property during the Application RUN TIME, e.g. price variable can store value of price for the specific Car instance (Object).

-----------------Reference types------------------
  • String
  • Array
  • Objects (including user defined data types such as Car)
Description:
Reference types are build from the Fundamental types or from other Reference types.
Below Reference type Car built from the combination of Fundamental type (double) and Reference type (Engine).

public class Car {
     private double price;
     private String currency;
}

Structure of the java class file (#1)


-----------Class and method Structure------------

public class Name {

//member variables  VAR_TYPE VAR_NAME; 

//method  

  modifier RetrunType methodName(){

    return VAR_NAME; 

  }

}


Description:
  • ReplaceName” with valid class Name such as Car.
  • VAR_TYPE = Fundamental or Reference data types. e.g. int, double, Car
  • VAR_NAME = Valid name, e.g. price, currency
  • modifier = public, private, protected, default (when not specified)
  • ReturnType = Fundamental or Reference data types. e.g. int, double, Car. Use void when method is not returning anything.
  • methodName = valid method name, e.g. getPrice()


-----------Source [Car.java]------------


public class Car {
private double price;
private String currency;
public String getCurrency() {
return currency;
}
public void setCurrency(String c) {
currency = c;
}
public double getPrice() {
return price;
}
public void setPrice(double p) {
price = p;
}
}


Description:
The Car class contains one private member variable (property) called price. As the price is private no other object can access or modify it. Hence, to access or modify the Car price, the Car class should expose the price to the external world via public methods  (interfaces) such as getPrice() and setPrice(). By using these interfaces other Objects can access and modify value of the price