Found 2620 Articles for Java

Why the constructor name is same as the class name in Java?

raja
Updated on 06-Feb-2020 10:09:33

2K+ Views

Every class object is created using the same new keyword, so it must have information about the class to which it must create an object. For this reason, the constructor name should be the same as the class name.Exampleclass MyConstructor{    public MyConstructor() {       System.out.println("The constructor name should be same as the class name");    }    public static void main(String args[]){       MyConstructor mc = new MyConstructor();    } }In the above program, the constructor name should be the same as the class name (MyConstructor).OutputThe constructor name should be same as the class name

Can we declare a main method as private in Java?

raja
Updated on 11-Feb-2020 07:49:21

4K+ Views

Yes, we can declare the main method as private in Java.It compiles successfully without any errors but at the runtime, it says that the main method is not public.Example:class PrivateMainMethod {    private static void main(String args[]){        System.out.println("Welcome to Tutorials Point");     } }The above code is working successfully at compile time but it will throw an error at the runtime.Output:Error: Main method not found in class PrivateMainMethod, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Can we execute a java program without a main method?

raja
Updated on 07-Oct-2023 02:45:53

24K+ Views

Yes, we can execute a java program without a main method by using a static block.  Static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by Java ClassLoader, It is also known as a static initialization block. Static initialization block is going directly into the stack memory. Example class StaticInitializationBlock{    static{       System.out.println("class without a main method");       System.exit(0);    } } In the above example, we can execute a java program without a main method (works until Java 1.6 version). ... Read More

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

raja
Updated on 17-Nov-2023 14:26:55

791 Views

Yes, we can create a class without a name using the Anonymous class. Anonymous class is an inner class which does not have a name and whose instance is created at the time of the creation of class itself and these classes are somewhat different from normal classes in its creation. Example public class Anonymous { public void show() {} public static void main(String args[]) { Anonymous a = new Anonymous() { public void show() { System.out.println("Anonymous Class"); } }; a.show(); } } Output Anonymous Class

How to implement an interface using an anonymous inner class in Java?

raja
Updated on 17-Nov-2023 08:41:15

1K+ Views

An anonymous inner class is a class which doesn't have a name, we will directly define it at the instantiation line. Example In the following program, we are implementing the toString() method of TutorialsPoint interface using Anonymous inner class and, print its return value. interface TutorialsPoint{ public String toString(); } public class Main implements TutorialsPoint { public static void main(String[] args) { System.out.print(new TutorialsPoint() { public String toString() { return "Welcome to Tutorials Point"; } }); } } Output Welcome to Tutorials Point

Interface variables are static and final by default in Java, Why?

raja
Updated on 11-Feb-2020 07:58:12

18K+ Views

An interface defines a protocol of behavior and not how we should be implemented. A class that implements an interface adheres to the protocol defined by that interface.Interface variables are static because java interfaces cannot be instantiated on their own. The value of the variable must be assigned in a static context in which no instance exists.The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned. In other words, interfaces can declare only constants, not instance variables.Template :interface interfaceName{    // Any number of final, static variables    datatype variableName = ... Read More

How many non-access modifiers are there in Java?

raja
Updated on 24-Feb-2020 08:07:53

123 Views

Java provides some other modifiers to provide the functionalities other than the visibility. These modifiers are called Non-Access ModifiersStatic  The members which are declared as static are common to all instances of a class. Static members are class level members which are stored in the class memory.Final  This modifier is used to restrict the further modification of a variable or a method or a class. The value of a variable which is declared as final can’t be modified once it gets a value. A final method can not be overridden in the subclass and you can not create a subclass ... Read More

How to compile & run a Java program using Command Prompt?

raja
Updated on 17-Nov-2023 14:04:11

16K+ Views

While many programming environments allow us to compile and run a program within the environment, we can also compile and run java programs using Command Prompt.After successful installation of JDK in our system and set the path, we can able to compile and execute Java programs using the command prompt. Step 1 − Need to create a java program either in Notepad or other IDE. Step 2 − Need to save this java file in a folder with "Demo.java" and it can be saved in a folder. Step 3 − Need to compile this java file from the command prompt using JAVAC command. Step ... Read More

What are the different steps involved to execute a Java program?

raja
Updated on 30-Jul-2019 22:30:26

2K+ Views

Java program execution follows 5 majors stepsEdit - Here the programmer uses a simple editor or a notepad application to write the java program and in the end give it a ".java" extension.Compile - In this step, the programmer gives the javac command and the .java files are converted into bytecode which is the language understood by the Java virtual machine (and this is what makes Java platform independent language). Any compile time errors are raised at this step.Load - The program is then loaded into memory. This is done by the class loader which takes the .class files containing ... Read More

Scope and lifetime of variables in Java?

raja
Updated on 06-Feb-2020 09:24:32

7K+ Views

Instance VariablesA variable which is declared inside a class and outside all the methods and blocks is an instance variable. The general scope of an instance variable is throughout the class except in static methods. The lifetime of an instance variable is until the object stays in memory.Class VariablesA variable which is declared inside a class, outside all the blocks and is marked static is known as a class variable. The general scope of a class variable is throughout the class and the lifetime of a class variable is until the end of the program or as long as the ... Read More

Advertisements