answersLogoWhite

0


Best Answer

Define your own class which will have all members final

& methods to return values of variable only

/*

javavishal.blogspot.com

This code shows the way to make a class immutable

*/

// The immutable class

final class VImmutableClass

{

// instance var are made private & final to restrict the access

private final int prop1;

// Constructor where we can provide the constant value

public VImmutableClass(int vprop1)

{

prop1 = vprop1

}

// provide only methods which return the instance var

// & not change the values

public int getProp1()

{

return prop1;

}

}

// class MyImmutable

public class MyImmutable

{

public static void main(String[] args)

{

VImmutableClass obj1 = new VImmutableClass(3);

System.out.println(obj1.getProp1());

// there is no way to change the values of count & value-

// no method to call besides getXX, no subclassing, no public access to var -> Immutable

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you create user defined immutable object in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What mutable and immutable in java and example program?

lmutable object


How to create immutable class in java?

by making that class final


Where can one watch immutable objects?

An immutable object is classed in Java programming as an object that cannot be changed after it is made. YouTube has videos that can be viewed on this subject as well as Vimeo.


What is a java object?

A java object is a collection of methods and properties defined in the Java programming language.


Can you create an object of interface in java?

maybe


What is string buffer in java?

StringBuffer is java class available in java.lang package which provides mutable String object where String is immutable class. The methods of this class like reverse(), append(),insert() gives facility to insert data of the same object.


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


Why object is the super class in java?

The answer to this is related to the idea of inheritance in general - the idea of inheritance is that you define a common set of behaviors, that apply to all subclasses. Anything defined in the "Object" class is available to all classes you create. Look in the documentation for the description of the "Object" class, to see what methods are available in all Java classes.


What is mutable in java?

It means that something can be changed after it is created. Immutable means that it can't be changed.


Can you create object in java at run time?

Yes, you just have to implement a method that creates a new object, and call it.


How oops concept is implemented in java?

OOP stands for Object Oriented Programming. Everything in Java is an Object. Any class you create extends the Object class by default thereby making everything in Java an object. Moreover, you can use features like Inheritance, Polymorphism, Encapsulation etc which are OOP concepts thereby making Java an Object Oriented Programming Language


Who create Java and when?

Who create Java & when? Why he create java ? What are mane functions of it?