One Go Question in JAVA

1- Can Constructor be final, static, and abstract
    No constructor can not be final , static and abstract -abstract and static for the obvious reason,
 if Final - cannot be inherited, But according to Java Specification constructor cannot be overridden
if Static - you can access it without object of the class but, it only execute when we create the object of class
if abstract - child class will provide its implementation, but not overridden so who will provide implementation

2-Can constructor be private
yes it used to create the singleton class, it will be used when we want no one can create its object

3-Can you overload main method
yes you can do easily but needs to change parameter compulsory

3.1-These All are valid method But no one is main method
public static void main(int[] args) {
System.out.println("main");
}
public static int main(String[] args) {
return 1;
}
public static void main(String args[] , String adr) {
System.out.println("faltu");
}
public static void main(String[] args, String[] adr) {
System.out.println("faltu");
}
public static void main( String adr,String args[] ) {

}

3.2-There are ony two correct definitions of main methods But can never have both together
public static void main(String[] args) {
}
public static void main(String... are) {
}

3.3-can main method be final 
     of-course

3.4- (String[] args) is null if we don't pass any command line variable.
      never its empty but not null

4-Explain JRE
Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java programs. It provides the minimum requirements for executing a Java application.

5-How will you call one constructor from another
using this() -- but the call to this or super must be the first statement of the constructor

6-Top level class can be private or protected
No it can only be public or no modifier
7-Can you assign this keyword to null
No Never Not

8- First Static block of base/child executes at the class load time then
  • then you need to create object of child class
  • base class initialization block
  • super class constructor 
  • initialization block of child class
  • child class constructor

9-why public static final or Enum does there is any difference.
enum is same as since it also have public static final all instance 
but enum  provides you various feature to compare to get value of  and other methods and also is singleton

10- Java Pass BY Value or Pass By references 
Java manipulates objects by reference, but it passes object references to methods by value." So it is always pass by value - BUT for objects, the value is the reference to the object, so that when you change an object that was given to a method, as a side effect it WILL be changed outside of the method too.


Java is always pass-by-value. Unfortunately, they decided to call pointers references, thus confusing newbies. Because those references are passed by value.
It goes like this:
public static void main( String[] args ){
    Dog aDog = new Dog("Max");
    foo(aDog);

    if (aDog.getName().equals("Max")) { //true
        System.out.println( "Java passes by value." );

    } else if (aDog.getName().equals("Fifi")) {
        System.out.println( "Java passes by reference." );
    }
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true

    d = new Dog("Fifi");
    d.getName().equals("Fifi"); // true
}
In this example aDog.getName() will still return "Max". The value aDog within main is not overwritten in the function foo with the Dog "Fifi" as the object reference is passed by value. If it were passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.
Likewise:
Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Fifi"); // true

public void foo(Dog d) {
    d.getName().equals("Max"); // true
    d.setName("Fifi");
}


Comments