7.Introduction to Classes and Objects
Tuesday, August 18, 2009
In this chapter you are going to learn what classes, objects, methods and fields are, and how to declare a class and use it to create an instance, how to declare methods in a class to implement the class’s behaviors etc.
Class and Object: Technically a class can be defined as “collection of fields and methods” and it can be used to create an instance for the class called object. This is one of the reasons Java is known as an object-oriented programming language.
An object has attributes that are carried with the object as it is used in a program. These attributes (fields) are specified as part of the object’s class.
We begin with an example that consists of classes Student and StudentTest.Class Student is used to display a message on the screen, where as class StudentTest is an application class in which the main method will use class Student.
Note I: We must compile the classes before we can execute the application.
Which means first of all we need to compile Student.java then StudentTest.javaas shown below
Javac Student.java StudentTest.java
Or you can use javac *.java (The asterisk (*) in .java indicates that all files in the current directory)
To see the output we must execute the main class as shown below
Java StudentTest then you will see the output as
Welcome to the Student Application
Note II: Only one public class is available in a single java source file.
Declaring a method with Parameter
In this fallowing example we are modifying the existing class Student, here we are changing the method which will accept a String parameter sname to pass Student Name from the main class.
In the StudentTest class we are creating an object for the Student.
Then invoking the showMessage method to pass Student Name as parameter at run time.
For this we are using Scanner class to input the student name at runtime.
Note: Class String is in package java.lang, which is imported implicitly into all source-code files.
Using Instance Variables and Methods
In above examples we declared all of an application’s variables in the application’s main method. Variables declared in the body of a particular method are known as local variables and can be used only in that method. When the method terminates, the values of its local variables are lost.
A class normally consists of one or more methods that manipulate the fields that belong to a particular object of the class. Fields are represented as variables in a class declaration. Such variables are called fields and are declared inside a class declaration but outside the bodies of the class’s method declarations. These are also known as instance variables.
The example in this section demonstrates a Student class that contains a sname field to represent a particular Student object’s student name.
In the fallowing example, we have used set and get methods. These methods are used to manipulate the private fields of a class. This convention is highly recommended in Java and is required for special software components called JavaBeans that can simplify programming in may Java integrated development environments (IDEs) like Netbeans and Eclipse.
The method that sets instance variable sname in this example called setSname, and the method that gets the value of instance variable sname is called as getSname.
When we compile this program you will get the fallowing output, here you can observe that the String initial value is set to null.
The Student Name is :null
Enter the Student Name :
Hrithik
The Student Name is :Hrithik
Class and Object: Technically a class can be defined as “collection of fields and methods” and it can be used to create an instance for the class called object. This is one of the reasons Java is known as an object-oriented programming language.
An object has attributes that are carried with the object as it is used in a program. These attributes (fields) are specified as part of the object’s class.
We begin with an example that consists of classes Student and StudentTest.Class Student is used to display a message on the screen, where as class StudentTest is an application class in which the main method will use class Student.
// Student.java
// Class declaration with a method
/**
*
* @author Sudarsan
*/
public class Student {
// This method is used to display a simple message on to the screen
public void showMessage()
{
System.out.println("Welcome to the Student Application");
}
}
// StudentTest.java
// Create a Student object and call its showMessage method.
/**
*
* @author Sudarsan
*/
public class StudentTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// std is an object for the class Student
Student std=new Student();
// Invoking the method by the help of object
std.showMessage();
}
}
Note I: We must compile the classes before we can execute the application.
Which means first of all we need to compile Student.java then StudentTest.javaas shown below
Javac Student.java StudentTest.java
Or you can use javac *.java (The asterisk (*) in .java indicates that all files in the current directory)
To see the output we must execute the main class as shown below
Java StudentTest then you will see the output as
Welcome to the Student Application
Note II: Only one public class is available in a single java source file.
Declaring a method with Parameter
In this fallowing example we are modifying the existing class Student, here we are changing the method which will accept a String parameter sname to pass Student Name from the main class.
// Student.java
// Class declaration with a method having an argument sname
/**
*
* @author Sudarsan
*/
public class Student {
// This method is used to display a simple message on to the screen
public void showMessage(String sname)
{
System.out.println("Welcome to :"+sname);
}
}
In the StudentTest class we are creating an object for the Student.
Then invoking the showMessage method to pass Student Name as parameter at run time.
For this we are using Scanner class to input the student name at runtime.
import java.util.Scanner;
// StudentTest.java
// Create a Student object and call its showMessage method.
/**
*
* @author Sudarsan
*/
public class StudentTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner s=new Scanner(System.in);
// std is an object for the class Student
Student std=new Student();
System.out.print("Enter the Student Name : ");
String name=s.next();
// Invoking the method by the help of object and passing name as parameter
std.showMessage(name);
}
}
Note: Class String is in package java.lang, which is imported implicitly into all source-code files.
Using Instance Variables and Methods
In above examples we declared all of an application’s variables in the application’s main method. Variables declared in the body of a particular method are known as local variables and can be used only in that method. When the method terminates, the values of its local variables are lost.
A class normally consists of one or more methods that manipulate the fields that belong to a particular object of the class. Fields are represented as variables in a class declaration. Such variables are called fields and are declared inside a class declaration but outside the bodies of the class’s method declarations. These are also known as instance variables.
The example in this section demonstrates a Student class that contains a sname field to represent a particular Student object’s student name.
In the fallowing example, we have used set and get methods. These methods are used to manipulate the private fields of a class. This convention is highly recommended in Java and is required for special software components called JavaBeans that can simplify programming in may Java integrated development environments (IDEs) like Netbeans and Eclipse.
// Student.java
// Class declaration with a method having an argument sname
/**
*
* @author Sudarsan
*/
public class Student {
private String sname; // sname for this Student
/**
* @return the sname
*/
public String getSname() {
return sname;
}
/**
* @param sname the sname to set
*/
public void setSname(String sname) {
this.sname = sname;
}
//Display a message to the Student user
public void showMessage()
{
System.out.println("The Student Name is :"+getSname());
}
}
The method that sets instance variable sname in this example called setSname, and the method that gets the value of instance variable sname is called as getSname.
import java.util.Scanner;
// StudentTest.java
// Create and manipulate a Student object and call its showMessage method.
/**
*
* @author Sudarsan
*/
public class StudentTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner s=new Scanner(System.in);
// std is an object for the class Student
Student std=new Student();
System.out.print("The Student Name is :"+std.getSname());
System.out.print("Enter the Student Name : ");
String name=s.next();
//set the StudentName
std.setSname(name);
// Invoking the method by the help of object and passing name as parameter
std.showMessage();
}
}
When we compile this program you will get the fallowing output, here you can observe that the String initial value is set to null.
The Student Name is :null
Enter the Student Name :
Hrithik
The Student Name is :Hrithik
Labels:
Classes and Objects
Please Provide Some More Info abt Classes