Found 2616 Articles for Java

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

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

833 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

132 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

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

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

How to set Temporary and Permanent Paths in Java?

raja
Updated on 11-Feb-2020 07:39:28

20K+ Views

There are two ways to set the path in java, First is Temporary Path and second is Permanent Path.Setting Temporary PathOpen command prompt in WindowsCopy the path of jdk/bin directory where java located (C:\Program Files\Java\jdk_version\bin)Write in the command prompt: SET PATH=C:\Program Files\Java\jdk_version\bin and hit enter command.Setting Permanent PathGo to My Computer ---> Right Click on it ---> Advanced System Settings ---> Advanced Tab ---> Click on Environment VariablesClick on New tab of User variables, assign value JAVA_HOME to Variable Namejava\jdk_version\bin path (copied path) to Variable Value and click on OK ButtonFinally, click on OK button.Read More

Java Program to check if two dates are equal

Samual Sam
Updated on 10-Aug-2023 12:47:18

160 Views

The date is a way of keeping track of our time as it is an integral part of our daily lives. In the programming world, there are a few scenarios where we are required to deal with the date and time such as while developing a calendar application and attendance management system in Java. Therefore, Java provides a few built-in classes like Date and LocalDate to work with date and time. In this article, we are going to explore Java programs to check whether two given dates are equal or not. Java Program to Check if two Dates are Equal ... Read More

Java Program to compare two sets

Krantik Chavan
Updated on 10-Aug-2023 12:59:11

749 Views

The Java Collection Framework provides an interface named Set that extends the Collection interface and serves to store unique elements. It depicts the features of a mathematical set. Hence, it allows all those operations that we can perform on a mathematical set such as union, comparison, intersection and so forth. The agenda of this article is to write Java programs to copare two sets. For the comparison operation of two sets, Java provides a built-in method 'equals()' that we will discuss in the next section. Java Program to Compare two Sets We are going to use the following class and ... Read More

Advertisements