Found 4338 Articles for Java 8

Can the main method in Java return a value?

Maruthi Krishna
Updated on 06-Aug-2019 08:26:53

1K+ Views

The public static void main(String args[]) is the entry point of a Java program Whenever you execute a program the JVM searches for the main method and starts executing the contents of it. If such method is not found the program gets executed successfully, but when you execute the program it generates an error.As the matter of fact you should declare the main method with public static as modifiers, void return type and String arguments if you change anything, JVM doesn’t considers as the entry point method and prompts an error at run time.Therefore, you cannot change the return type ... Read More

What happens when you declare a method/constructor final in Java?

Maruthi Krishna
Updated on 08-Aug-2019 13:23:52

336 Views

Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass’s final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class).Still, if you try to override a final method a compile time error is generated.Exampleinterface Person{    void dsplay(); } class Employee implements Person{    public final void dsplay() {       System.out.println("This is display method of the Employee class");    } } class Lecturer extends Employee{    public void dsplay() {       System.out.println("This is display method of ... Read More

Can we have integers as elements of an enum in Java?

Maruthi Krishna
Updated on 06-Aug-2019 08:15:03

8K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Integers as elements of an enumNo, we can have ... Read More

What is a variable, field, property in Java?

Maruthi Krishna
Updated on 06-Aug-2019 08:10:05

1K+ Views

In programming to hold data members we use variables, Java you can declare three types of variables namely, Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within ... Read More

What are the modifiers allowed to use along with local variables in Java?

Maruthi Krishna
Updated on 02-Jul-2020 14:24:08

973 Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within a class, ... Read More

How to include current date when logging exceptions to a file with FileOutputStream in java?

Maruthi Krishna
Updated on 06-Aug-2019 07:50:57

261 Views

There are several logging frame works available to log your data into files. You can also define your own method. In either cases to add the current time to your logged exception you can use the LocalDateTime class.It is an immutable class representing the date-time, it stores date-time as year-month-day-hour-minute-second. The now() method of this class returns the current date-time.Using this method concatenate the current date and time to your exception message and write to your required file.Exampleimport java.io.FileOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.util.Arrays; import java.util.Scanner; public class LoggingToFile {    private static void writeLogToFile(Exception e) throws IOException { ... Read More

What is a MalformedURLException and how to fix it in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:26:42

17K+ Views

While working with client-server programming in Java (JSE), if you are using java.net.URL class object in your program, you need to instantiate this class by passing a string representing required URL to which you need to establish connection. If the url you have passed in the string which cannot be parsed or, without legal protocol a MalformedURLException is generated.ExampleIn the following Java example we are tring to get establish a connection to a page and publishing the response.We have tampered the protocol part, changed it to htt, which should be http or, https.import java.util.Scanner; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; ... Read More

What happens if an exception is not handled in a java program?

Maruthi Krishna
Updated on 02-Jul-2020 14:28:11

3K+ Views

An exception is an issue (run time error) occurred during the execution of a program. For understanding purpose let us look at it in a different manner.Generally, when you compile a program, if it gets compiled without a .class file will be created, this is the executable file in Java, and every time you execute this .class file it is supposed to run successfully executing each line in the program without any issues. But, in some exceptional cases, while executing the program, JVM encounters some ambiguous scenarios where it doesn’t know what to do.Here are some example scenarios −If you ... Read More

Do all properties of an Immutable Object need to be final in Java?

Maruthi Krishna
Updated on 02-Jul-2020 14:29:20

950 Views

Immutable class/object is the one whose value cannot be modified. For example, Strings are immutable in Java i.e. once you create a String value in Java you cannot modify it. Even if you try to modify, an intermediate String is created with the modified value and is assigned to the original literal.Defining immutable objectsWhenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.ExampleFollowing Java program demonstrates the ... Read More

Why variables defined in try cannot be used in catch or finally in java?

Maruthi Krishna
Updated on 02-Jul-2020 14:30:03

5K+ Views

A class in Java will have three kinds of variables namely, static (class), instance and, local.Instance variables − These variables belong to the instances (objects) of a class. These are declared within a class but outside methods. These are initialized when the class is instantiated. They can be accessed from any method, constructor or blocks of that particular class.Class/static variables − class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the ... Read More

Advertisements