+92 332 4229 857 99ProjectIdeas@Gmail.com

Classes In Java


The Structure of Classes

class name  {
            declaration
           constructor definitions
           method definitions
}
Note: These parts of a class can actually be in any order

A Complete Class Example:


Let’s implement a small example to give you an idea of classes in java….
// Student.java

/* Demonstrates the most basic features of a class. 
A student is defined by their name and rollNo.
There are standard get/set accessors for name and rollNo.
NOTE: A well-documented class should include an introductory comment like this.
Don't get into all the details – just introduce the landscape.*/

public class Student {
             
    private String name;
    private int rollNo;
        
        // Standard Setters
             
       // Note the masking of class level variable name
       public void setName (String _name) {
                   this.name = _name;
               }

       // Note the masking of class level variable rollNo
       public void setRollNo (int _rollNo) {
                   if (_rollNo > 0) {          
                       this.rollNo = _rollNo;
                    } else {
                       this.rollNo = 100;
                   }              
               }
              
        // Standard Getters
         public String getName ( ) {
                  return name;
        }
 
         public int getRollNo ( ) {
                  return rollNo;              
        }     

       // Constructor that uses a default value instead of taking an argument.
        public Student() {
                 name = "not set";
                 rollNo = 100;
        }

       // parameterized Constructor for a new student object
        public Student(String name, int rollNo) {
                 setName(name);//call to setter of name
                 setRollNo(rollNo);//call to setter of rollNo
              }
             
     // Copy Constructor for a new student object
         public Student(Student s) {
               name = s.name;
               rollNo = s.rollNo;
         }
             
    // method used to display student name & rollNo on console
          public void print () {
      System.out.println("Student name:" + name + ", roll no:" + rollNo);
   }
  }// end of class

public static void main(String[] args) {
       // Make two Student Objects
       Student s1 = new Student("ali", 15);//call to parameterized constructor   
       Student s2 = new Student(); //call to default constructor

       s1.print();//call to print() method of class Student
       s2.print();  

       s2.setName("Rehan");
       s2.setRollNo(20);
         
       System.out.print("Student name:" + s2.getName());
       System.out.println(" rollNo:" + s2.getRollNo());

       System.out.println("calling copy constructor");  
       Student s3 = new Student(s2); //call to copy constructor
           
       s2.print();
       s3.print();
          
       s3.setRollNo(-10);//RollNo would be set to 100 Because it is lessthan 0

       s3.print();  
}

Output:

Student name : ali, rollNo : 15
Student name : not set, rollNo : 100
Student name : Rehan rollNo : 20
Calling Copy Constructor
Student name : Rehan, rollNo : 20
Student name : Rehan, rollNo : 20
Student name : Rehan, rollNo : 100

0 comments: