Write a Java Program to Create an Immutable Class

Java Program to Create an Immutable Class

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Create an Immutable Class
Java Program to Create an Immutable Class

Write a Java Program to Create an Immutable Class

PROGRAM: Java Program to Create an Immutable Class

/*Java Program to Create an Immutable Class*/

// class is declared final
final class Immutable {

// private class members
private String name;
private int date;

Immutable(String name, int date) {

// class members are initialized using constructor
this.name = name;
this.date = date;
}

// getter method returns the copy of class members
public String getName() {
return name;
}

public int getDate() {
return date;
}

}

class Main {
public static void main(String[] args) {

// create object of Immutable
Immutable obj = new Immutable(“Programiz”, 2011);

System.out.println(“Name: ” + obj.getName());
System.out.println(“Date: ” + obj.getDate());
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *