Lecture 2
Lecture 2
(KCS-602)
Lecture 2
Object and class example
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
Output: 0
System.out.println(s1.name); null
}
}
main outside the class
//Java Program to demonstrate having the main method in
//another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
Output: 0
System.out.println(s1.id);
null
System.out.println(s1.name);
}
}
Ways to initialize object
1. By reference variable
2. By method
3. By constructor
By reference variable
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name=“Balagurusamy";
System.out.println(s1.id+" "+s1.name);//printing members with a white
space
} Output: 101 Balagurusamy
}
By method
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n; }
void displayInformation(){System.out.println(rollno+" "+name); } }
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
Output: 111 Karan
s1.insertRecord(111,"Karan"); 222 Aryan
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation(); } }
Constructor
In order to overload a method, the argument lists of the methods must differ in
either of these:
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)
Example 1: Overloading – Different Number of parameters in argument list
class DisplayOverloading
{ public void disp(char c)
{
System.out.println(c); }
public void disp(char c, int num) {
System.out.println(c + " "+num);
} }
class Sample
{ public static void main(String args[])
{ DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10); Output:a
} }
a 10
Example 2: Overloading – Difference in data type of parameters
class DisplayOverloading2
{ public void disp(char c)
{ System.out.println(c); }
public void disp(int c)
{ System.out.println(c ); } }
class Sample2
{ public static void main(String args[])
{ DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5); } }
Output : a
5
Example3: Overloading – Sequence of data type of arguments
class DisplayOverloading3
{ public void disp(char c, int num)
{ System.out.println("I’m the first definition of method disp” +c);
}
public void disp(int num, char c)
{ System.out.println("I’m the second definition of method disp” + c );
} }
class Sample3
{ public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
Output: I’m the first definition of method disp 51
obj.disp(52, 'y'); }} I’m the second definition of method disp 52
Constructor overloading
Usage of java this keyword: Here is given the 6 usage of java this keyword.
Static keyword can be used with class, variable, method and block. Static members belong
to the class instead of a specific instance, this means if you make a member static, you
can access it without object.
The static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class
Java static variable
If you declare any variable as static, it is known as a static variable.
The static variable can be used to refer to the common property of all objects (which is not unique
for each object), for example, the company name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time of class loading.
Advantages of static variable: It makes your program memory efficient (i.e., it saves
memory).
Understanding the problem without static variable
class Student{
int rollno;
String name;
String college="ITS";
}
Suppose there are 500 students in my college, now all instance data members will get memory
each time when the object is created. All students have its unique rollno and name, so instance
data member is good in such case. Here, "college" refers to the common property of all objects. If
we make it static, this field will get the memory only once.
//Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college =“ABESEC";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n; }
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);} }
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){ Output: 111 Karan ABESEC
222 Aryan ABESEC
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college=“ABES";
s1.display();
Java static method
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a
class.
A static method can access static data member and can change the value of it.
Example of static method
//Java Program to get the cube of a given number using the static method
class Calculate{
static int cube(int x){
return x*x*x; }
public static void main(String args[]){
Output: 125
int result=Calculate.cube(5);
System.out.println(result); } }
Java static block