Found 4335 Articles for Java 8

Can we declare an abstract method final or static in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:36:04

10K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring abstract method staticIf you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.If you still, try to declare an abstract method static a compile time error is generated saying ... Read More

Can we create an object for the abstract class in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:36:39

1K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Instantiating an abstract classOnce a class is abstract it indicates that it may contain incomplete methods hence you cannot create an object of the abstract class.If you try ... Read More

Can we define an abstract class without abstract method in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:37:31

5K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.And yes, you can declare abstract class without defining an abstract method in it. Once you declare a class abstract it indicates that the class is incomplete ... Read More

Is it possible to catch multiple Java exceptions in single catch block?

Maruthi Krishna
Updated on 29-Jun-2020 13:39:07

955 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Multiple exceptions in a codeBefore Java 7 whenever we have a code that may generate more than one exception and if you need to handle the specifically you should use multiple catch blocks on a single try.ExampleThe following Java program contains an array of numbers (which is displayed). from user it accepts two positions from this array and, divides the number in first position ... Read More

While working with Java in eclipse I got a warning saying "dead code". What does it mean?

Maruthi Krishna
Updated on 29-Jun-2020 13:30:28

863 Views

A code block/statement in Java to which the control never reaches and never gets executed during the lifetime of the program is known as unreachable block/statement.public class UnreachableCodeExample {    public String greet() {       System.out.println("This is a greet method ");       return "Hello";       System.out.println("This is an unreachable code ");    }    public static void main(String args[]) {       new UnreachableCodeExample().greet();    } }Dead codeA dead code is an unreachable code, but it doesn’t generate compile time error. But if you execute it in eclipse it gives you a warning.ExampleIn ... Read More

What are unreachable blocks in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:32:06

378 Views

A code block/statement in Java to which the control never reaches and never gets executed during the lifetime of the program is known as unreachable block/statement.Generally, it happens whenever a piece of code hasA return statement before it.An infinite loop before it.Java does not support unreachable code. If you have any such statements, (which are unreachable) Java compiler raises a compile time error.Example1In the following Java program, the class UnreachableCodeExample has a method named greet which returns a String value. After the return statement it has a print statement. Live Demopublic class UnreachableCodeExample {    public String greet() {     ... Read More

When and where static blocks are executed in java?

Maruthi Krishna
Updated on 30-Jul-2019 22:30:26

2K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodExecuting a static blockJVM first looks for the main method (at least the latest versions) and then, starts executing the program including static ... Read More

Is there any alternative solution for static constructor in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:33:19

433 Views

The main purpose of constructors in Java is to initialize the instance variables of a class.But, if a class have Static variables you cannot initialize them using the constructors (you can assign values to static variables in constructors but in that scenario, we are just assigning values to static variables). because static variables are loaded into the memory before instantiation (i.e. before constructors are invoked)So, we should initialize static variables from static context. We cannot use static before constructors, Therefore, as an alternation to you can use static blocks to initialize static variables.Static blockA static block is a block of ... Read More

What is the difference between equals() method and == operator in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:13:39

702 Views

The equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Example Live Demopublic class Sample{    public static void main(String []args){       String s1 = "tutorialspoint";       String s2 = "tutorialspoint";       String s3 = new String ("Tutorials Point");       System.out.println(s1.equals(s2));       System.out.println(s2.equals(s3));    } }Outputtrue falseYou can also compare two strings using == operator. But, it compares references of the given variables not ... Read More

What is overloading? What happens if we overload a main method in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:16:56

6K+ Views

Overloading is one of the mechanisms to achieve polymorphism. If a class contains two methods with the same name and different parameters, whenever you call this method the method body will be bound with the method call based on the parameters.ExampleIn the following Java program, the Calculator class has two methods with name addition. The only difference between them is that one contains 3 parameters and the other contains 2 parameters.Here, we can call the addition method by passing two integers or three integers. Based on the number of integer values we pass, the respective method will be executed. Live Demopublic ... Read More

Advertisements