Interviww - sivakrsna/Java-Interview GitHub Wiki
- It is a java file(class) which doesn't extend or implement any other java file(class).
- Plain Old Java Object is a Java object not bound by any restriction other than those forced by the Java Language Specification. I.e., a POJO should not have to Extend prespecified classes Implement prespecified interface Contain prespecified annotations
- It is a java file(class) in which all variables are private, methods are public and appropriate getters and setters are used for accessing variables.
- All properties private (use getters/setters)
- A public no-argument constructor Implements Serializable.
- It is a java file(class) which may consist of public/private/default/protected variables and which may or may not extend or implement another java file(class).
You can access the element of an array directly by its index value Eg. Arrays, ArrayList
A blank final variable in Java is a final variable that is not initialized during declaration.
A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.
This is useful to create immutable objects:
public class Bla {
private final Color color;
public Bla(Color c) {this.color = c};
}
Bla is immutable (once created, it can't change because color is final). But you can still create various Blas by constructing them with various colors.
Static methods and variables are defined as belonging to the class and not specifically to an instance. They are common to all instances, and are usually accessed through the class name rather than a specific instance.
Generics are also known as parameterized types. When you use generics with the collections classes, the compiler is being informed to restrict the collection to allow only certain types to be contained.
Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.
- The required classes for reflection are provided under java.lang.reflect package.
- Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
- Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.
One very common use case in Java is the usage with annotations. JUnit 4, for example, will use reflection to look through your classes for methods tagged with the @Test annotation, and will then call them when running the unit test.
The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. Reflection is then the ability to make modifications at runtime by making use of introspection. The distinction is necessary here as some languages support introspection, but do not support reflection. One such example is C++
- A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.
- Implementing Comparable means "I can compare myself with another object." This is typically useful when there's a single natural default comparison.
- A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface
- Implementing Comparator means "I can compare two other objects." This is typically useful when there are multiple ways of comparing two instances of a type - e.g. you could compare people by age, name etc.
The static keyword can be used in 4 scenarios static variables static methods static blocks of code static nested class
Let's look at static variables and static methods first.
Static variable
- It is a variable which belongs to the class and not to object (instance).
- Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.
- A single copy to be shared by all instances of the class.
- A static variable can be accessed directly by the class name and doesn’t need any object. Syntax: Class.variable
Static method
- It is a method which belongs to the class and not to the object (instance).
- A static method can access only static data. It can not access non-static data (instance variables) unless it has/creates an instance of the class.
- A static method can call only other static methods and can not call a non-static method from it unless it has/creates an instance of the class.
- A static method can be accessed directly by the class name and doesn’t need any object. Syntax: Class.methodName()
- A static method cannot refer to this or super keywords in anyway.
Static class
- Java also has "static nested classes". A static nested class is just one which doesn't implicitly have a reference to an instance of the outer class.
- Static nested classes can have instance methods and static methods. There's no such thing as a top-level static class in Java.
Side note
main method is static since it must be be accessible for an application to run before any instantiation takes place.
Final is used to apply restrictions on class, method and variable. Final keyword is used in several different contexts to define an entity which cannot later be changed.
- A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.
- A final method can't be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.
- A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a blank final variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared; otherwise, a compile-time error occurs in both cases.
Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.
When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. Once it has been assigned, the value of the final variable cannot change.
There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:
- Using new keyword This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();
- Using Class.forName() If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
- Using clone() The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
- Using object deserialization Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
- Using newInstance() method of Constructor class There is one newInstance() method in the java.lang.reflect.Constructor class which we can use to create objects.
Constructor<Employee> constructor = Employee.class.getConstructor();
Employee emp3 = constructor.newInstance();
System is a final class in the java.lang package.
out is a static member of the System class, and is an instance of java.io.PrintStream.
println is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.