Learn about how to create a class and object in Java. Edufect (www.edufect.com) provides Perfect Java, the best Java course. Edufect is the best institute for Java, Android, Python.
1. 91-9582167401 EDUFECT
JT-M-CLASA
www.edufect.com PERFECT JAVA
- 1 -
Defining a class
Classes are defined with class keyword followed by <class-name>. Class names should start with
capital letter.
Syntax:
1 class <class_name> //you can use any name for your class except keywords
2 {
3 //body of class
4 //define variables here that will be shared between objects of this class
5 }
Example 1: A simple class named Student
6 class Student
7 {
8 String name;
9 int rollNo;
10 }
Creating objects
In java new keyword is used to create an object.
Syntax:
<class_name> object_name = new <class_name>();
Example 2: Creating object of our Student class
Student st = new Student();
Accessing variables defined inside a class
OR
What is .(dot) operator in java? And how to use it?
.(dot) is an operator. It is used to access variables defined in a class through its object. We cannot
directly access these variables.
Syntax:
object-name.variable = value;
Example 3: Create an object of Student class and give values to variables defined in this class.
1 Student obj = new Student();
2 obj.name = "Rohit";
3 obj.rollNo = 5;
4 System.out.println(obj.name);
5 System.out.println(obj.rollNo);
Output:
Rohit
5
Explanation: In this example we are accessing the variable "name" and "rollNo" with dot operator
and assigning values to them. Now name is Rohit and rollNo is 5.
2. 91-9582167401 EDUFECT
JT-M-CLASA
www.edufect.com PERFECT JAVA
- 2 -
Default values of variables in class
After object creation every variables gets initialized with default value based on its data type.
Data type Default value
boolean false
char 'u0000'
int 0
double 0.0
Any reference type (eg. String) null
Example 4: Default Values
1 Student obj = new Student();
2
3 System.out.println(obj.name); //before any values are assigned
4 System.out.println(obj.rollNo);
5
6 obj.name = "Vivek"; //after giving values
7 obj.rollNo = 50;
8
9 System.out.println(obj.name);
10 System.out.println(obj.rollNo);
Output:
null
0
Vivek
50
Creating multiple objects of a class
Just like variables you can create multiple objects.
Example 5: Multiple Objects
Class:
1 class StudentWithMarks
2 {
3 String name;
4 int marks;
5 int rollNo;
6 }
Code:
1 StudentWithMarks st1, st2;
2 st1 = new StudentWithMarks();
3 st2 = new StudentWithMarks();
4
5 st1.name = "Sanchit";
6 st1.rollNo = 10;
7 st1.marks = 90;
8
9 st2.name = "Mohit";
3. 91-9582167401 EDUFECT
JT-M-CLASA
www.edufect.com PERFECT JAVA
- 3 -
10
11 System.out.println(st1.name);
12 System.out.println(st1.rollNo);
13 System.out.println(st1.marks);
14
15 System.out.println(st2.name);
16 System.out.println(st2.rollNo); //default value
17 System.out.println(st2.marks); //default value
Output:
Sanchit
10
90
Mohit
0
0
Example 6: Create a class Point that contains two fields x and y of type int.
1 class Point
2 {
3 int x;
4 int y;
5 }
Example 7: Create two objects of class Point. Give them random values and print these values
1 Point p1 = new Point();
2 p1.x = 4;
3 p1.y = 1;
4
5 Point p2 = new Point();
6 p2.x = 6;
7 p2.y = 5;
8
9 System.out.println("Point 1 ");
10 System.out.println("x = "+p1.x);
11 System.out.println("y = "+p1.y);
12 System.out.println("Point 2 ");
13 System.out.println("x = "+p2.x);
14 System.out.println("y = "+p2.y);
Output:
Point 1
x = 4
y = 1
Point 2
x = 6
y = 5