Sunday, 12 August 2018

Why can't i use static variable in java constructor?

Example:
class Student{
Student() {
static int i = 0;
System.out.println(i++); 
}
public static void main(String args[]){
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
}

Here, Why can't i use static variable in java constructor?

Ans:
If you want to declare static variable then declare it outside of the constructor, at class level like this -
public class Student{
private static int i;
}
You declaration of static occurred at your constructor which is a local variable and local variable can not be static. And that's why you are getting - illegal modifier for parameter i. And finally for initializing static variable you may use a static initialization block (though it's not mandatory) -
public class Student{
private static int i;
static {
i = 5;
}
}

Tuesday, 7 August 2018

JAVA 10 exciting Feature : Local Variable Type Inference

JAVA 10 :  Local Variable Type Inference
Like JavaScript, Kotlin, and Scala, now Java 10 introduced var keyword that allows you to declare a local variable without specifying its type. The type will be inferred from context. For example, when you say var name = "Silan", then the compiler will already know the type is String.
The var keyword can only be used for local variables, i.e. variables inside methods or code blocks — you cannot use it for member variable declaration inside the class body.
But like Python, it doesn't make Java a dynamically typed language. Java is still a statically typed language, and once the type is assigned, you cannot change it. For example, var name = "Silan" is ok, but if we will assign name =100; then it is not right.
This is one of the most eye-catching features of Java 10. It reduces the amount of boilerplate code needed to declare local variables in Java.
Overall we can say in a single statement that we can declare variables without specifying the associated type. A declaration such as:
List<String> l=new ArrayList<String>();
Can be written as var l=new ArrayList<String>();

Note: Type Inference is not a new Concept in JAVA but it is a new concept for local variables. 

Tuesday, 3 July 2018

Conversion from String to StringBuffer and StringBuilder

    Here in this context We can directly pass String class object to StringBuffer and StringBuilder class constructors. As String class is immutable in java, so for editing a string, we can perform same by converting it to StringBuffer or StringBuilder class objects. 

Example.java
package java8s;

public class Example {

          public static void main(String[] args) {
                  
                   String str="SILAN";
                  
                   //Conversion from String object to StringBuffer
                   StringBuffer sb1=new StringBuffer(str);
                  
                   System.out.println(sb1);
                  
                   sb1.reverse();
                   System.out.println(sb1);
                  
                   //Conversion from String object to StringBuilder
                    StringBuilder sb2=new StringBuilder(str);
                   sb2.append("Technology");
                  
                   System.out.println(sb2);
                                     

          }

}

Output:
SILAN 
NALIS
SILANTechnology

Thursday, 28 June 2018

Why main() in JAVA is public !!

Answer :
              main() in JAVA is public due to the following two reasons:

1. main() method can be accessed by any no. of classes.
2. main() method can be invoked by JVM from everywhere.


Note: The keyword public is a modifier.

Monday, 26 March 2018

Does finally always execute in Java?

Yes, finally will be called.
The only times finally won't be called are:
  1. If you invoke System.exit();
  2. If the JVM crashes first;
  3. If there is an infinite loop (or some other non-interruptable, non-terminating statement) in the try block;
  4. If the OS forcibly terminates the JVM process.
  5. If the host system dies; e.g. power failure, hardware error, etc.

Is Java “pass-by-reference” or “pass-by-value”?

Java is always pass-by-value. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners.

Example;

public static void main( String[] args ) {
    Dog aDog = new Dog("Max");
    // we pass the object to foo
    foo(aDog);
    // aDog variable is still pointing to the "Max" dog when foo(...) returns
    aDog.getName().equals("Max"); // true, java passes by value
    aDog.getName().equals("Fifi"); // false 
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true
    // change d inside of foo() to point to a new Dog instance "Fifi"
    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 changed 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.

Can main() method overloaded ??

Yes, main() can be overloaded. Let’s see the following examples for better clarity :

ExampleMain.java

class ExampleMain
{
public static void main(String[] args)
{
System.out.println("Hiii");
}

public static void main(int x)
{
System.out.println("Hello");
}

}

Output
Hiii