Maruthi Krishna has Published 951 Articles

Is there any alternative solution for static constructor in java?

Maruthi Krishna

Maruthi Krishna

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

432 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). ... Read More

What are unreachable blocks in java?

Maruthi Krishna

Maruthi Krishna

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

377 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 ... Read More

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

Maruthi Krishna

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";     ... Read More

What are the different ways to print an exception message in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 13:27:07

8K+ 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.Printing the Exception messageYou can print the exception message in Java using one of ... Read More

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

Maruthi Krishna

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 ... Read More

Different ways to create an object in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 13:15:48

669 Views

In Java a class is a user defined datatype/blue print in which we declare methods and variables.public class Sample{ }Once you declare a class you need to create an object (instantiate) it. You can create an object using various ways −Using new keywordIn general, an object is created using the ... Read More

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

Maruthi Krishna

Maruthi Krishna

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

699 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){       ... Read More

Does main() method accept arguments other than string array, in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 13:08:04

4K+ Views

The public static void main() method accepts an array of values of the type String Java from the user.public class{    public static void main(String[] args){    } }You can pass them at the time of execution right after the class name separated with spaces as −java ClassName 10 20 ... Read More

Can we change return type of main() method in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 13:05:29

3K+ Views

The public static void main() method is the entry point of the Java program. Whenever you execute a program in Java, the JVM searches for the main method and starts executing from it.You can write the main method in your program with return type other than void, the program gets compiled ... Read More

Why main() method must be static in java?

Maruthi Krishna

Maruthi Krishna

Updated on 29-Jun-2020 13:02:18

2K+ Views

Static − If you declare a method, subclass, block, or a variable static it is loaded along with the class.In Java whenever we need to call an (instance) method we should instantiate the class (containing it) and call it. If we need to call a method without instantiation it should ... Read More

Advertisements