JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Question: How can you create an immutable class in java?


Answer:

Here are the steps to create immutable class:

Declare the class as final, we can not extend the final class.

public final class MyTestImmutable { ... }

Declare all fields as final. Final fields can not be changed once its assigned.

private final int salary;

Do not provide any method which can change the state of the object, for example the setter methods which changes the values of the instance variables.

The “this” reference is not allowed to escape during construction from the immutable class and the immutable class should have exclusive access to fields that contain references to mutable objects like arrays, collections and mutable classes like Date etc by:
Declaring the mutable references as private.
Not returning or exposing the mutable references to the caller.

<< Previous Question | Next Question >>

List Of Java Interview Questions:

  1. How garbage collector knows that the object is not in use and needs to be removed?
  2. Can Java thread object invoke start method twice?
  3. Give the list of Java Object class methods.
  4. Can we call servlet destory() from service()?
  5. Can we override static method?
  6. Can you list serialization methods?
  7. What is the difference between super() and this()?
  8. How to prevent a method from being overridden?
  9. Can we create abstract classes without any abstract methods?
  10. How to destroy the session in servlets?
  11. Can we have static methods in interface?
  12. What is transient variable?
  13. Incase, there is a return at the end of try block, will execute finally block?
  14. What is abstract class or abstract method?
  15. What is default value of a boolean?
  16. When to use LinkedList or ArrayList?
  17. What is daemon thread?
  18. Does each thread in java uses seperate stack?
  19. What is the difference between Enumeration and Iterator?
  20. Find out switch statement output.
  21. Does system.exit() in try block executes finally block?
  22. What is fail-fast in java?
  23. What is final, finally and finalize?
  24. In java, are true and false keywords?
  25. What are the different session tracking methods?
  26. What is the purpose of garbage collection?
  27. What are the types of ResultSet?
  28. What is difference between wait and sleep methods in java?
  29. What is servlet context?
  30. What happens if one of the members in a class does not implement Serializable interface?
  31. What is race condition?
  32. How to get current time in milli seconds?
  33. How can you convert Map to List?
  34. What is strictfp keyword?
  35. What is System.out in Java?
  36. What is difference between ServletOuptputStream and PrintWriter?
  37. What is java static import?
  38. When to use String and StringBuffer?
  39. What is difference between StringBuffer and StringBuilder?
  40. What is wrapper class in java?
  41. Is Iterator a Class?
  42. What is java classpath?
  43. Can a class in java be private?
  44. Is null a keyword in java?
  45. What is the initial state of a thread when it is started?
  46. What is the super class for Exception and Error?
  47. What is Class.forName()?
  48. Can interface be final?
  49. What is the difference between exception and error?
  50. What is default value of a local variables?
  51. What is local class in java?
  52. Can we initialise uninitialized final variable?
  53. Can we declare abstract method as final?
  54. Can we have finally block without catch block?
  55. What is pass by value and pass by reference?
  56. Can we declare main method as private?
  57. What is the difference between preemptive scheduling and time slicing?
  58. Can non-static member classes (Local classes) have static members?
  59. What are the environment variables do we neet to set to run Java?
  60. Can you serialize static fields of a class?
  61. What is the difference between declaring a variable and defining a variable?
  62. Where can we use serialization?
  63. What modifiers are allowed for methods in an Interface?
  64. What is the purpose of Runtime and System class?
  65. Which one is faster? ArrayList or Vector? Why?
  66. What is the difference between static synchronized and synchronized methods?
  67. What is the order of catch blocks when catching more than one exception?
  68. What is the difference between the prefix and postfix forms of the increment(++) operator?
  69. What is hashCode?
  70. What is the difference between Hashtable and HashMap?
  71. What are the restrictions when overriding a method?
  72. What is the use of assert keyword?
  73. What is adapter class?
  74. What is difference between break, continue and return statements?
  75. What is the difference between while and do-while statements?
  76. When does the compiler provides the default constructor?
  77. Difference between C++ and Java.
  78. Usages of java packages.
  79. What is dynamic class loading?
  80. What happens if you do not provide a constructor?
  81. Difference between shallow cloning and deep cloning of objects?
  82. Can we have interfaces with no defined methods in java?
  83. What is the difference between “==” and equals() method?
  84. How can you create an immutable class in java?
  85. What are access modifiers?
  86. Can we have private constructor in java?
  87. Why do we need generics in java?
  88. What is the difference between a product and a project?
  89. How does substring() method works on a string?
  90. What is the difference between a Java Library and a framework?
  91. Next set of java interview questions
Knowledge Centre
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.