What is a class ?
a. Class helps us to generate objects
b. Class will generate object whenever we make a request to the class using new keyword
What is new keyword ?
a. It send request to the class to create object
b. Once the class has created an object. new keyword gets the address of the object and stores
that in a reference variable
Syntax to create a class
class Name {
}
Syntax to create object:
className variableName = new className();
Example 1:
public class A {
public static void main(String[] args){
A a1 = new A();
System.out.println(a1);
A a2 = new A();
System.out.println(a2);
A a3 = new A();
System.out.println(a3);
}
}
Output:
A@7960847b
A@6a6824be
A@5c8da962
Static keyword versus non static in java ?
Non static members
a. These members are loaded into the object whenever an object is created
b. Without creating an object non static member cannot be accessed
Example 1:
public class A {
int i = 10; //non static
public static void main(String[] args){
A a1 = new A();
System.out.println(a1.i);
}
}
Output:
10
Example 2:
public class A {
int i = 10; //non static
int j = 100;//non static
public static void main(String[] args){
A a1 = new A();
System.out.println(a1.i);
System.out.println(a1.j);
}
}
Output:
10
100
Static members:
a. These members belongs to the class. To access these members do not create object
b. These members are loaded into the common memory of the class only once
Example 1:
public class A {
static int i = 10; //static
static int j = 100;//static
public static void main(String[] args){
System.out.println(A.i);
System.out.println(A.j);
}
}
Output:
10
100
Example 2:
public class A {
int i = 10;//belongs to object
static int j = 100; //static (Belongs to class)
public static void main(String[] args){
A a1 = new A();
System.out.println(a1.i);
System.out.println(A.j);
}
}
Output:
10
100
Example 3:
public class A {
int i = 10;//belongs to object
public static void main(String[] args){
A a1 = new A();
a1.i = 100;
System.out.println(a1.i);
A a2 = new A();
System.out.println(a2.i);
}
}
Output:
100
10
Overview of methods:
1. Methods will execute only when we call it
Example 1:
public class A {
public static void main(String args[]) {
A a1 = new A();
a1.test();
}
public void test(){
System.out.println(500);
}
}
Output:
500
Example 2:
public class A {
public static void main(String args[]) {
A.test();
}
public static void test(){
System.out.println(500);
}
}
Output:
500
Example 3:
public class A {
public static void main(String args[]) {
A a1 = new A();
a1.test();
}
public void test(){//non static--class
System.out.println(100);
}
}
Output:
100
Example 4:
public class A {
public static void main(String args[]) {
A.test();
}
public static void test(){//static--class
System.out.println(100);
}
}
Output:
100
Types Of Variables in Java
There are four types of variables in java
1. Local variables
2. static variables
3. non-static variables
4. reference variables
Local Variables:
a. Local variables are created inside a method
b. Local variables can be used / accessed only within created method
c. Local variables are accessed directly with its name
d. Local variables cannot be used without initializing
Example 1:
public class A {
public static void main(String args[]) {
A a1 = new A();
a1.test();
}
public void test(){
int i = 10;//Created
System.out.println(i);//Using
}
}
Example 2:
public class A {
public static void main(String args[]) {
System.out.println(i);
A a1 = new A();
a1.test();
}
public void test(){
int i = 10;//Created
System.out.println(i);//Using
}
}
Output:
Error because we are using variable i outside the test() method
Example 3:
public class A {
public static void main(String args[]) {
int i = 10;//created
System.out.println(i);//using
A a1 = new A();
a1.test();
}
public void test(){
System.out.println(i);//using outside main
}
}
Output:
Error
Example 4:
public class A {
public static void main(String args[]) {
int i = 10;//created
System.out.println(i);//using
}
}
Output:
10
Example 5:
public class A {
public static void main(String args[]) {
int i;//created
System.out.println(i);//using
}
}
Output:
/A.java:4: error: variable i might not have been initialized
System.out.println(i);//using
^
1 error
Static variables
a. Static variables are created outside all the methods but inside a class using static keyword
b. These variables can be used anywhere in the program
c. Static variables can be accessed in three ways
1. ClassName.variableName
2. variableName
3. referenceVariable.memberName
4. It is not mandatory to initialize static variables, if we do not initialize then depending on the
data type auto initialization would happen
Example 1:
public class A {
//here static variables are created
static int i = 10;
public static void main(String args[]) {
//Here local variables are created
System.out.println(A.i);
A a1 = new A();
a1.test();
}
public void test(){
System.out.println(A.i);
}
}
Output:
10
10
Example 2:
public class A {
public static void main(String args[]) {
static int i = 10;
}
}
Output:
Error because static variables cannot be created inside a method
Example 3:
public class A {
static int i = 10;
public static void main(String args[]) {
System.out.println(A.i);
System.out.println(i);
A a1 = new A();
System.out.println(a1.i);//A.i
}
}
Output:
10
10
10
Example 4:
public class A {
static int i;//0
public static void main(String args[]) {
System.out.println(A.i);
}
}
Output:
0
Non Static variables"
a. Non static variables created outside all the methods but inside a class without static keyword
b. These variables belongs to the object and only after object creation these variables can be
accessed
c. It is not mandatory to initialize non static variables, if we do not initialize then depending on the
data type auto initialization would happen
Example 1:
public class A {
//static variable is created here
//nonstatic varoable is also created here
int i = 10;
public static void main(String args[]) {
//Local variable is created here
A a1 = new A();
System.out.println(a1.i);
}
}
Output:
10
Example 2:
public class A {
//static variable is created here
//nonstatic varoable is also created here
int i ;
public static void main(String args[]) {
//Local variable is created here
A a1 = new A();
System.out.println(a1.i);
}
}
Output:
0
Reference Variables:
a. The main purpose of reference variables is to store objects address
b. Data type for a reference variable is class name in java
Reference variables in java is categorized as
1. Local reference Variable:
a. These variables are created inside a method and should be used only within created method
b. Local reference variables if used without initializing it then we would get an error
Example 1:
public class A {
public static void main(String args[]) {
A a1 = new A();
System.out.println(a1);
}
public void test(){
System.out.println(a1);
}
}
Output:
Error, Because a1 is created in main method and should be used only within main method
Example 2:
public class A {
public static void main(String args[]) {
A a1 ;
System.out.println(a1);
}
}
Output:
Error
2. static reference variables:
a. These variables are created outside all the methods, but inside a class using static keyword.
b. These variables can accessed anywhere in the program
c. If we do not initialize static reference variables then automatically null value will be stored in it
Example 1:
public class A {
static A a1 = new A();//Global access
public static void main(String args[]) {
System.out.println(a1);
a1.test();
}
public void test(){
System.out.println(a1);
}
}
Output:
A@7960847b
A@7960847b
Example 2:
public class A {
static A a1;
public static void main(String args[]) {
System.out.println(a1);
}
}
Output:
null
Unary Operators in Java
1. ++
2. --
a. POST INCREMENT:
Here we increment the value of the variable by one when next time in the
program we see the same variable again
Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ + i++;
System.out.println(i);
System.out.println(j);
}
}
Output:
12
21
Example 2:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ + i++ + i++;
System.out.println(i);
System.out.println(j);
}
}
Output:
13
33
Example 3:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ ;
System.out.println(i);
System.out.println(j);
}
}
Output:
11
10
Example 4:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ ;
System.out.println(i);
System.out.println(j);
}
}
//9632882052
Output:
12
32
b. PRE INCREMENT:
Here we increment the value of the variable in same step
Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
int j = ++i + ++i ;
System.out.println(i);
System.out.println(j);
}
}
Output:
12
23
Example 2:
public class A {
public static void main(String args[]) {
int i = 10;
int j = ++i + ++i + ++i;
System.out.println(i);
System.out.println(j);
}
}
Output:
13
36
Example 3:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i++ + ++i + i++;
System.out.println(i);
System.out.println(j);
}
}
Output:
13
34
Example 4:
c. POST DECREMENT:
here we decrement the value of the variable by one next time when we see the same variable
Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
int j = i-- + i--;
System.out.println(i);
System.out.println(j);
}
}
//9632882052
Output:
8
19
d. PRE DECREMENT:
Here we decrement the value of the variable in same step.
Example 1:
public class A {
public static void main(String args[]) {
int i = 10;
int j = --i + --i;
System.out.println(i);
System.out.println(j);
}
}
Output:
8
17