Found 9150 Articles for Object Oriented Programming

Can we define a package after the import statement in Java?

Vivek Verma
Updated on 28-Aug-2025 16:13:17

653 Views

No, we cannot define a package after the import statement in Java. The compiler will throw an error if we try to insert a package after the import statement. A package is a group of similar types of classes, interfaces, and sub-packages. To create a class inside a package, declare the package name in the first statement in our program. Important! If you use the package statement in an online compiler, you may still get an error even if it is defined correctly. This is because many online compilers do not support custom package structures. Example In the following example, ... Read More

What changes has been introduced in JDK7 related to Exception handling in Java?

Maruthi Krishna
Updated on 07-Aug-2019 09:17:57

102 Views

Since Java 7 try-with resources was introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)The resources we declare in the try block should extend the java.lang.AutoCloseable class.ExampleFollowing program demonstrates the try-with-resources in Java.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying {    public static void main(String[] args) {       try(FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt"));       FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))){          byte[] buffer = new byte[1024];     ... Read More

Are the instances of Exception checked or unchecked exceptions in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:16:03

596 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Checked Exception − A checked exception is an exception that occurs at ... Read More

Why we can't initialize static final variable in try/catch block in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:11:01

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.Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.Static methods in try-blockIn the same way, static variables belong to the class and can be accessed anywhere within the class, which contradicts with the definition of the local variable. Therefore, declaring ... Read More

How can we decide that custom exception should be checked or unchecked in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:02:42

6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions.To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format.MyException(String msg){    super(msg); } Or, public String toString(){    return ... Read More

What is a Type-safe Enum in Java?

raja
Updated on 28-Aug-2025 14:47:43

2K+ Views

In Java, an enum is a special class that represents a "group of constants" that cannot be changed, just like a "final variables". Java provides an enum keyword to create an enum, and inside the class, the constants are written in uppercase letters and separated by commas (, ). Syntax to create an enum in Java: enum EnumName { CONST1, CONST2, CONST3 // ... } Here, enum: A predefined keyword used to define an enum. EnumName: The name ... Read More

Access property as a property using 'get' in JavaScript?

Manisha Patil
Updated on 23-Aug-2022 13:31:54

539 Views

Property accessors offer dot notation or bracket notation-based access to an object's properties. Associative arrays are a good way to understand objects (a.k.a. map, dictionary, hash, lookup table). The names of the property names are the keys inside this array. A difference between properties and methods is frequently made when discussing an object's properties. But the distinction between a property and a technique is simply an established practice. When a property's value is a reference to an example of a Function, for example, that property can be invoked to perform a function. Syntax ObjectName[propertyName] You will study JavaScript getter ... Read More

How can we Implement a Queue using Stack in Java?

Vivek Verma
Updated on 22-Jul-2025 11:44:33

2K+ Views

This article will discuss how to implement a Queue using a Stack in Java. It will briefly introduce the Queues and Stacks, and provide a suitable example that shows their implementation. Queues in Java In Java, the Queue class extends the Collection interface, and it supports the insert and remove operations using a FIFO (First-in-First-Out). The last element added to the end of the queue can become the first element to be removed from the queue. The following diagram will give you a better understanding of the Queue: Stacks in Java In Java, a Stack is a subclass (child class) ... Read More

How can we convert character array to a Reader in Java?

Vivek Verma
Updated on 28-Aug-2025 16:19:10

424 Views

Converting a character array to a reader in Java means treating the array as a stream of characters to be read sequentially. Let's quickly learn about character array and its creation in Java: In Java, a character array is an array that holds elements of only char type, and it is also used to store a sequence of characters. Following is the syntax to define a character array in Java: char char_array_name[] = {'char1', 'char2', 'char3'....} or char char_array_name[] = new char[size]; Here, char_array_name: The name of the character array. ... Read More

Latitude and longitude of the user's position in JavaScript?

Manisha Patil
Updated on 23-Aug-2022 13:43:06

4K+ Views

The user's current location can be found using the getCurrentPosition() method. The Geolocation API that HTML5 offers us allows us to know the user's geographic location. The Geolocation API provides the latitude and longitude of the user's current location, that are thereafter delivered to the backend via JavaScript and displayed on the website. The read-only longitude property of the GeolocationCoordinates interface is a double-precision floating point value that indicates a location's longitude, expressed in decimal degrees. The GeolocationCoordinates object is a component of the GeolocationPosition interface, since it is the object type delivered by Geolocation API calls which collect and ... Read More

Advertisements