Saturday, April 22, 2023

Static, Final and Access Modifier

 

Static Keyword

The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

 

Final Keyword

Java final keyword is a non-access specifier that is used to restrict a class, variable, and method. If we initialize a variable with the final keyword, then we cannot modify its value.

 

Difference between Static and Final

Static                                                                         Final

 

Static keyword can be applied to a                            Final keyword can be applied

Nested class, method and block                                  to method variable and

As well as variable.                                                         classes.

 

Static variable can be change after                             Final variable can't be change

Initialization.                                                                    after initialization.

 

Static method and variable can                                 Object can created to call final

Be accessed directly by class name                            method or final variable.

And does not Object belong to the

Class.

 

Access Modifiers

Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class.

 

1.     Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

2.     Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.

3.     Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.

4.     Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

 

Difference between this and super keyword

 

this

super

In Java, this keyword is a reference variable that refers to the current object or instance of a class.

In Java, super keyword is a reference variable that refers to the superclass's object or instance of a class.

It can access variables and methods of the current class.

It can access variables and methods of the parent class or superclass.

Default constructor of the current class can be invoked by this.

Default constructor of the parent class can be invoked by super.

this() can be passed in the method call as an argument.

super() is returned with no arguments.

this() can be passed in the method call as an argument.

 

 

Friday, April 21, 2023

Hibernate Interview Question

  What is hibernate ? 

Hibernate is an open source lightweight ORM tools that is used to store, 

manipulate, retrieve data from the database 

 

What is ORM ? 

ORM is an  acronym for Object Relational Mapping. It is a programming strategy to map object with data stored in database. It is simplifies data creation, data manipulation, and data access. 

 

What are the core interface for Hibernate ? 

  1. Configuration 

  2. Session Factory 

  3. Session 

  4. Query 

  5. Criteria 

  6. Transaction 

 

What is Session Factory ? 

Session Factory provides the instance of Session. It is a factory of Session. It holds the data of second level cache that is not enabled by default. 

 

Is Session Factory a thread-safe object? 

Yes, Session Factory is a thread-safe object, many threads cannot access it simultaneously. 

 

What is a Session in Hibernate? 

A session is an object that maintains the connection between Java object application and database. Session also has methods for storing, retrieving, modifying or deleting data from database using methods like persist(), load(), get(), update(), delete(), etc. Additionally, It has factory methods to return Query, Criteria, and Transaction objects. 

 

Is Session a thread-safe object? 

No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads. 

 

What is the difference between get and load method? 

No. 

get() 

load() 

1) 

Returns null if an object is not found. 

Throws Object Not Found Exception if an object is not found. 

2) 

get() method always hit the database. 

load() method doesn't hit the database. 

3) 

It returns the real object, not the proxy. 

It returns proxy object. 

4) 

It should be used if you are not sure about the existence of instance. 

It should be used if you are sure that instance exists. 

 

 

How to make an immutable class in hibernate? 

If you mark a class as mutable="false", the class will be treated as an immutable class. By default, it is mutable="true". 

 

Static, Final and Access Modifier

  Static Keyword The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blo...