Java Classes

  Home  Java Programing  Java Classes


“Java Classes frequently Asked Questions in various Java Classes related job Interviews by interviewer. Get preparation of Java Classes job interview questions”



76 Java Classes Questions And Answers

43⟩ Obtaining objects of a class is a ____ -step processA) oneB) TwoC) ThreeD) Four

Explanation: Obtaining objects of a class is a two-step process. First, you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual, physical copy of the object and assign it to that variable.

two step process

 137 views

44⟩ What is singleton class and how can we get it from a general class,explain with examples(program)?

In computer science, the singleton design pattern is designed to restrict instantiation of a class to one (or a few) objects. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist.

The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If one does exist it returns a reference to the object that already exists. To make sure that the object cannot be instantiated any other way the constructor is made either private or protected.

The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one.

The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated.

A Java programming language solution is as follows. It is based on the Q&A link found below, modified for multi-threading, however, it is still vulnerable to the double-checked locking anti-pattern, also found below:

public class Singleton {

private static Singleton INSTANCE = null;

// Private constructor suppresses

// default public constructor

private Singleton() {}

//synchronized creator to defend against multi-threading issues

//another if check here to avoid multiple instantiation

private synchronized static void createInstance() {

if (INSTANCE == null) {

INSTANCE = new Singleton();

}

}

public static Singleton getInstance() {

if (INSTANCE == null) createInstance();

return INSTANCE;

}

}

 186 views

46⟩ What is the significance of wait() method with respect to Object class, not in Thread class?

the wait() method is defined in the java.lang.OBject() class and not in the java.lang.Thread()class. it's signature is public static void wait(). we do have some overloaded versions of the wait() method. when an wait method is called by an thread on a object it release the lock on the object and goes to an WAIT/BLOCKED state inorder to let the other threads to enter the RUNNING state. However it regains the Lock on the object after sometime when it moves from the WAIT/BLOCKED state to RUNNABLE state . that can happen due to the call of notify(),notifyAll() methods by other threads holding of the respective objects Lock.

 139 views

56⟩ What is Class Loader?

Class loaders are one of the cornerstones of the Java virtual machine (JVM) architecture. They enable the JVM to load classes without knowing anything about the underlying file system semantics, and they allow applications to dynamically load Java classes as extension modules.A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.

 132 views

60⟩ The Code in java is contained within MethodsA) TrueB) False

Source Code is normally an entire file.

In Java the actual execution (or result preparation) would be there in methods only.Method will return exact one return value / 0(void).

Methods will change the behaviour of an object (Operations).

Fields contains value or state of an object.

 125 views