java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
STEP 1 : We save it has Main.java
STEP 2 : we right click the folder screen then click terminal in terminal we enter -- javac Main.java , to compile the file . after that Main.class file will create.(main.class) -- this called byte code.
Main.java
│
▼
Java Compiler (javac)
│
▼
Main.class
STEP3 : JVM, Now run , java Main
Main.class
│
▼
JVM
│
▼
Machine Code
│
▼
CPU
Why not directly Machine Code?
Before JAVA,
C, C++, pascal these are only the programing language was used before java, then had some problem what is
1.Platform dependencies(software + hardware = plateform)
Here developer need to create separate version for different systems. this was taken time, cost and maintenance.
After java :
Write Once, Run Anywhere
Why should file name be Main.java?
The public class name and filename must be the same.
Main.java
public class Main
Hello.java
public class Main , this will cause error
Program for how use non static variable
class Product {
String name;
int price;
}
public class Index {
public static void main(String[] args) {
Product product1 = new Product();
Product product2 = new Product();
Product product3 = new Product();
product1.name = "Laptop";
product1.price = 50000;
product2.name = "Mobile";
product2.price = 20000;
product3.name = "Headphone";
product3.price = 3000;
System.out.println(product1.name + " : " + product1.price);
System.out.println(product2.name + " : " + product2.price);
System.out.println(product3.name + " : " + product3.price);
}
}
here java have two variable one is static variable and another one is non static variable , non static variable need object.
class Product {
String name;
int price;
}
This is not public class , this class name is Product , always the class name should be in start with caps, that class have two non static variable one is name and another one is price, i did not give any value , so in default string have null value in that variable and int have 0 value in default .
then we create Public class Index {,
In class Index have main method , if i want to create any static variable then should create it only outside of main method or inside Index class . in this case we did not created any static variable , so we will going to inside main method , already we know that we have two non static variable in product class, we need to use that, but before that we need to create object to allocate space to use that in diff object,
Product product1 = new Product();
Product product2 = new Product();
Product product3 = new Product();
product1.name = "Laptop";
product1.price = 50000;
product2.name = "Mobile";
product2.price = 20000;
product3.name = "Headphone";
product3.price = 3000;
how to allocate space, class variable is the key thing to create , we two non static variable in product class right, so we can able to create multiple object to create multiple space ,
first class name ,
product product1 = new product()
product
product()-- these two are class names where we create non static variable ,
product1 is variable we can give any name
after that product 1 is the object to use non static variable
so create,
product1.name = "laptop"
product1.price = 50000;
plaintext
Here we assigned a value for name and price only for product1,
now we can able to print
System.out.println(product1.name + " : " + product1.price);
plaintext
we can able to create many object based on the needs.
In another same program
public class Index {
String name;
int price;
public static void main(String[] args) {
Index product1 = new Index();
Index product2 = new Index();
Index product3 = new Index();
product1.name = "Laptop";
product1.price = 50000;
product2.name = "Mobile";
product2.price = 20000;
product3.name = "Headphone";
product3.price = 3000;
System.out.println(product1.name + " : " + product1.price);
System.out.println(product2.name + " : " + product2.price);
System.out.println(product3.name + " : " + product3.price);
}
}
plaintext
what the diff is ?
class Product {
String name;
int price;
}
This only the main difference , here we create the non static variable in same class .
Differences between static and non static variables
| Static | Non-static |
|---|---|
| One copy for the whole class | One copy per object |
| Shared | Not shared |
| Access using class name | Access using object |
Top comments (0)