Found 9150 Articles for Object Oriented Programming

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

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

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

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

317 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

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

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

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

How can we Implement a Stack using Queue in Java?

Vivek Verma
Updated on 22-Jul-2025 12:42:53

1K+ Views

This article will discuss how to implement a Stack using a Queue in Java.  Stack in Java In Java, a Stack is a subclass (child class) of the Vector class, and it represents a LIFO stack of objects, which stands for Last-in-First-Out. The last element added at the top of the stack (In) can be the first element to be removed (Out) from the stack. The following diagram will give you a clear idea about the Stack: Queue in Java In Java, the Queue class extends the Collection interface, and it supports the insert and remove operations using a FIFO, ... Read More

How many ways to make an object eligible for GC in Java?

raja
Updated on 23-Nov-2023 09:07:55

491 Views

The process of destroying unreferenced objects is called a Garbage Collection(GC). Once an object is unreferenced it is considered as an unused object, hence JVM automatically destroys that object. There are various ways to make an object eligible for GC. By nullifying a reference to an object We can set all the available object references to "null" once the purpose of creating an object is served. Example public class GCTest1 { public static void main(String [] args){ String str = "Welcome to TutorialsPoint"; // String object referenced by variable str and it is not eligible for GC yet. ... Read More

Advertisements