Programming Articles - Page 2617 of 3366

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

547 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

963 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

7K+ 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

Different ways to create an object in java?

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

1K+ 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 new keyword as −Sample obj = new Sample();ExampleIn the following Java we have a class with name sample, which has a method (display). From the main method we are instantiating the Sample class and invoking the display() method. Live Demopublic class Sample{    public void display() {       System.out.println("This ... Read More

Can We declare main() method as Non-Static in java?

Maruthi Krishna
Updated on 29-Jun-2020 12:59:51

7K+ Views

The public static void main(String ar[]) method is the entry point of the execution in Java. When we run a .class file JVM searches for the main method and executes the contents of it line by line.You can write the main method in your program without the static modifier, the program gets compiled without compilation errors. But, at the time of execution JVM does not consider this new method (without static) as the entry point of the program.  It searches for the main method which is public, static, with return type void, and a String array as an argument.public static int main(String[] ... Read More

Can we declare main() method as private or protected or with no access modifier in java?

Maruthi Krishna
Updated on 29-Jun-2020 13:00:49

7K+ Views

Java provides various access specifiers namely private, public and protected etc...The Private modifier restricts the access of members from outside the class. A class and interface cannot be public.The Public access modifier can be associated with class, method, constructor, interface, etc. public can be accessed from any other class.The protected access modifier can be associated with variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The default access modifier does not have keyword a variable or method declared ... Read More

Why main() method must be static in java?

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 be static. Moreover, static methods are loaded into the memory along with the class.In the case of the main method, it is invoked by the JVM directly, so it is not possible to call it by instantiating its class. And, it should be loaded into the memory along with the ... Read More

Is it possible to create a class without a name in Java?

Shriansh Kumar
Updated on 16-May-2025 15:51:21

1K+ Views

Yes, we can create a class without a name in Java using the anonymous class. It is a type of inner class which does not have a name and whose instance is created at the time of the creation of the class itself. You can create this class in a single statement using the new keyword. Creating multiple instances of this class is not allowed. You can use anonymous classes in case where you need to override methods of a class or an interface for a one-time use, and you don't want to create a separate named class for it. ... Read More

Can we declare constructor as final in java?

Maruthi Krishna
Updated on 29-Jun-2020 12:51:54

4K+ Views

A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have the same name as their class and, have no return type.There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.Example Live Demopublic class Example {    public Example(){       System.out.println("This is the constructor of the class example");    }    public static void main(String args[]) {       Example obj = new Example();    } }OutputThis is the constructor of the class exampleFinal methodWhenever you ... Read More

Advertisements