Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

15. Getters, Setters and Constructors in Java

 

Getters and Setters

  • Getter: Returns the value.
  • Setter: Sets/updates the value.
  • Getter and setter are methods used to protect our data and make our code more secure.
  • To protect our data, we make our variables private in a class. So these variables can't be accessed directly outside the class. So we use getters and setters to set the value of that variable and to return the value of that variable.
 import java.util.Scanner;

class Student {
    private int rollNo; // Cannot be accessed in other class by creating object of the class
    private String name;
    private double marks;
    private long phone;

    public void setRollNo(int rollNo) { // Setter
        this.rollNo = rollNo;
    }

    public int getRollNo() { // Getter
        return this.rollNo;
    }

    public void setName(String user) { // Setter
        this.name = user;
    }

    public String getName() { // Getter
        return name;
    }

    public void setMarks(double marksobtained) { // Setter
        marks = marksobtained;
    }

    public double getMarks() { // Getter
        return this.marks;
    }

    public void setPhone(long contact) { // Setter
        phone = contact;
    }

    public long getPhone() { // Getter
        return phone;
    }
}

class GettersSetters {
    public static void main(String[] arr) {
        Student st1 = new Student();
        st1.setRollNo(25);
        st1.setName("Pankaj Singh Negi");
        st1.setMarks(95.6);
        st1.setPhone(7249926519l);

        Scanner input = new Scanner(System.in);
        Student st2 = new Student();
        System.out.print("Enter your name: ");
        st2.setName(input.nextLine());
        System.out.print("Enter your roll no: ");
        st2.setRollNo(input.nextInt());
        System.out.print("Enter your marks: ");
        st2.setMarks(input.nextDouble());
        System.out.print("Enter your contact number: ");
        st2.setPhone(input.nextLong());

        System.out.println("\nPrinting details of student 1...");
        System.out.println("Student Name: " + st1.getName());
        System.out.println("Student Roll No: " + st1.getRollNo());
        System.out.println("Student Marks: " + st1.getMarks());
        System.out.println("Student Phone Number: " + st1.getPhone());

        System.out.println("\nPrinting details of student 2...");
        System.out.println("Student Name: " + st2.getName());
        System.out.println("Student Roll No: " + st2.getRollNo());
        System.out.println("Student Marks: " + st2.getMarks());
        System.out.println("Student Phone Number: " + st2.getPhone());
        input.close();
    }
}

Output :

Enter your name: Deepak Chandra Sharma
Enter your roll no: 38
Enter your marks: 92.6
Enter your contact number: 8791304163

Printing details of student 1...
Student Name: Pankaj Singh Negi
Student Roll No: 25
Student Marks: 95.6
Student Phone Number: 7249926519

Printing details of student 2...
Student Name: Deepak Chandra Sharma
Student Roll No: 38
Student Marks: 92.6
Student Phone Number: 8791304163

Note: this keyword is used to refer current class instance variables.
To invoke current class constructor and method.
To return the current class instance.

Constructors

  • A constructor in Java is a special method that is used to initialize objects.
  • Constructors do not return any type of data i.e. that do not have any return data type.
  • Every time an object is created using new() keyword, at least one constructor is called.
  • If we do not create a constructor by yourself, then a default constructor (created by Java compiler) is called.

Rules for creating a Constructor

  • The constructor name should be same as the class name.
  • It must have no explicit return type.
  • We can use access modifiers while declaring a constructor, but a Java constructor cannot be abstract, static, final and synchronized.

Types of Constructors

  1. Default constructor: A constructor with zero parameters is called a default constructor.
    The default constructor is used to provide the default values to the object like 0, null etc., depending on the type.
  2. Parameterized constructor: A constructor which has a specific number of parameters is called a parameterized constructor.
    The parameterized constructor is used to provide different values to distinct objects. However, we can provide the same values also.
import java.util.Scanner;

class Student {
    private int rollNo;
    private String name;
    private double marks;
    private long phone;

    public Student() {
        this.rollNo = 420;
        this.name = "Satyam Chaurasia";
        this.marks = 98.9;
        this.phone = 8299416025l;
    }

    public Student(int rollNo, String user, double marksobtained, long contact) {
        this.rollNo = rollNo;
        name = user;
        this.marks = marksobtained;
        phone = contact;
    }

    public Student(double marksobtained, int rollNo) {
        this.rollNo = rollNo;
        name = "Aviskar Chaudhary";
        marks = marksobtained;
        phone = 9454978593l;
    }

    public int getRollNo() { // Getter
        return this.rollNo;
    }

    public String getName() { // Getter
        return name;
    }

    public double getMarks() { // Getter
        return this.marks;
    }

    public long getPhone() { // Getter
        return phone;
    }

}

class Constructors {
    public static void main(String[] arr) {
        Student st1 = new Student();

        Scanner input = new Scanner(System.in);
        System.out.print("Enter your roll no: ");
        int rn = input.nextInt();
        input.nextLine();
        System.out.print("Enter your name: ");
        String user = input.nextLine();
        System.out.print("Enter your marks: ");
        double marks = input.nextDouble();
        System.out.print("Enter your phone number: ");
        long contact = input.nextLong();
        Student st2 = new Student(rn, user, marks, contact);

        Student st3 = new Student(95.4, 21);

        System.out.println("\nPrinting details of student 1...");
        System.out.println("Student Name: " + st1.getName());
        System.out.println("Student Roll No: " + st1.getRollNo());
        System.out.println("Student Marks: " + st1.getMarks());
        System.out.println("Student Phone Number: " + st1.getPhone());

        System.out.println("\nPrinting details of student 2...");
        System.out.println("Student Name: " + st2.getName());
        System.out.println("Student Roll No: " + st2.getRollNo());
        System.out.println("Student Marks: " + st2.getMarks());
        System.out.println("Student Phone Number: " + st2.getPhone());

        System.out.println("\nPrinting details of student 3...");
        System.out.println("Student Name: " + st3.getName());
        System.out.println("Student Roll No: " + st3.getRollNo());
        System.out.println("Student Marks: " + st3.getMarks());
        System.out.println("Student Phone Number: " + st3.getPhone());
        input.close();
    }
}

Output :

Enter your name: Deepak Chandra Sharma
Enter your marks: 92.6
Enter your phone number: 8791304163

Printing details of student 1...
Student Name: Satyam Chaurasia
Student Roll No: 420
Student Marks: 98.9
Student Phone Number: 8299416025

Printing details of student 2...
Student Name: Deepak Chandra Sharma
Student Roll No: 38
Student Marks: 92.6
Student Phone Number: 8791304163

Printing details of student 3...
Student Name: Aviskar Chaudhary
Student Roll No: 21
Student Marks: 95.4
Student Phone Number: 9454978593


Previous

Next

Comments