← Back to Book Detail

6 Classes in Java — Java Programming (6/7) -- Java Programming

Browse
85%

6 Classes in Java — Java Programming

6 Classes in Java — Java Programming 6.1 Introduction 6.2 A class A class is a template for an object. Classes gather methods that provide functionality for objects. An object is an instance of the class. The data or variables defined within a class are called members of the class. The class usage and definition of class is similar to its definition in C++. 6.2.1 A simple class A class definition with three variables is given below. class student{ //class name int rollno; //class member float marks1, marks2; //class members } 6.2.2 The constructor To create a student object named student_1, call the constructor student(). student student_1=new student(); //new creates, student() constructs An object like student_1 is a class variable. A constructor is a special member function for automatically creating an instance of a class. The function has the same name as class. To use an object in Java, we should first create the object and make a variable refer to the object. The new command is used to create objects. In addition, we should construct the object we are creating. A constructor is a method used to create new objects that initializes the values of the object. For example, Assume we have a class of objects Sample_date representing dates, with corresponding year, month and day. Hence, the constructor needs three values as shown below. Sample_date date=new Sample_date(1999,12,1); The parameters are given within the brackets. We should refer to the class documentation or the class definition if information about parameters become necessary. A few constructors like student() as given above may have no parameters at all. Program 6.1 Program to define student and calculate the student’s total marks. //studClass.java //Click here for code class student { int rollno; float marks1, marks2; } class studClass{ public static void main(String args[]) { student student1=new student(); student student2=new student(); float total; student1.marks1=82; student2.marks2=79; total=student1.marks1+student2.marks2; System.out.println("total marks="+total); } } //Output: total marks=161.0 A constructor defines the action to be done when an object of a class is created. If no explicit constructor is specified then Java will automatically supply a default constructor. This is the case with student class. Runtime Exception occurs if new is unable to allocate memory. The difference between class and object is that a class creates a new data type that can be used to create objects. That is, a class creates a logical framework that defines the relationship between its members. When we declare an object of a class, we are creating an instance of that class. Thus, a class is a logical constant. An object has physical reality (object occupies space in memory). student student1=new student(); //create student1 object using a constructor student student2 student1; // create a student2 object using variable declaration student1=null; // we make the object refer to null. In the ab
← Previous Chapter Next Chapter →