Object Oriented Programming Articles

Page 510 of 589

Handling IllegalComponentStateException in java.

Maruthi Krishna
Maruthi Krishna
Updated on 06-Aug-2019 471 Views

It is the sub class of IllegalStateException, This indicates that AWT component is not in an appropriate state i.e. if you are working with components but, not using them properly leads to this exception. There are several scenarios where this exception occursExampleIn the following example we are trying to build a sample login form here after setting the visibility of the window to true, we are trying to set the location by platform true which is inappropriate.import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginDemo extends JFrame implements ActionListener {    JPanel panel;    JLabel user_label, password_label, message;    JTextField ...

Read More

Different scenarios that cause NoSuchElementException in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 06-Aug-2019 555 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurs the program gets terminated abruptly and, the code past the line that generated the exception never gets executed. Each exception is represented by its respective class.NosuchElement ExceptionThis is a Runtime exception i.e. it occurs at the execution time.While accessing the contents of a collection, array or other objects using the accessor methods of an Enumeration, Iterator or, tokenizer, such as next() or nextElement(), if you try to get the elements from an empty object, or if you try to get the ...

Read More

How to create an immutable set in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Aug-2019 1K+ Views

Whenever 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.A Set is an interface in collection framework, which does not allow duplicate values.You need to keep following points in mind while creating an immutable set −We should not be able to add or delete elements from it.we should not be able to add null values to an immutable set.Once you create an immutable set you cannot add ...

Read More

What is a variable, field, property in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 06-Aug-2019 2K+ 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 06-Aug-2019 371 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

How to access Static variable of Outer class from Static Inner class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 05-Aug-2019 1K+ Views

A class with in another class is known as inner class, you cannot declare a class static unless it is an inner class. A static inner class is just like other class variables. You can access it (static inner class) without instantiationExampleYou can access the static variable of an outer class just using the class name. Following Java example demonstrates how to access static variables of a class from a static inner class.public class Outer {    static int data = 200;    static class InnerDemo {       public void my_method() {          System.out.println("This is ...

Read More

Why interfaces don't have static initialization block when it can have static methods alone in java?

Maruthi Krishna
Maruthi Krishna
Updated on 05-Aug-2019 2K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.A static method is declared using the static keyword and it will be loaded into the memory along with the class. You can access static methods using class name without instantiation.Static methods in an interface since java8Since Java8 you can have static methods in an interface (with body). You need to call them using the name of the interface, just like static methods of a class.ExampleIn the following example, we are defining a static method in an interface and accessing ...

Read More

How to access the object of a class without using the class name from a static context in java?

Maruthi Krishna
Maruthi Krishna
Updated on 05-Aug-2019 756 Views

The only possible solution is to get the stack trace of the current thread. Get the class name using an element in the stack trace. Pass it to the forName() method of the class named Class.This returns a Class object and you can get an instance of this class using the newInstance() method.Examplepublic class MyClass {    String name = "Krishna";    private int age = 25;    public MyClass() {       System.out.println("Object of the class MyClass");       System.out.println("name: "+this.name);       System.out.println("age: "+this.age);    }    public static void demoMethod() throws Exception {   ...

Read More

Are values returned by static method are static in java?

Maruthi Krishna
Maruthi Krishna
Updated on 05-Aug-2019 3K+ Views

Whenever you return values from a static method they are either static nor instance by default, they are just values.The user invoking the method can use them as he wants. i.e. you can retrieve the values and declare them static.But, since you cannot declare variables of a method static if you need to declare the vales returned by a method static you need to invoke it in the class outside the methods.ExampleAssume we have a class with name Demo as −class Demo{    int data = 20;    public Demo(int data){       this.data = data;    }   ...

Read More

How to resolve javac is not recognized as an internal or external command in java?

Maruthi Krishna
Maruthi Krishna
Updated on 05-Aug-2019 3K+ Views

When you compile a program if you see this error it indicates that either you have not installed Java in your system properly or, you haven’t set the Path variable.The Path variable − The path environment variable is used to specify the set of directories which contains executional programs.When you try to execute a program from command line, the operating system searches for the specified program in the current directly, if available, executes it.In case the programs are not available in the current directory, operating system verifies in the set of directories specified in the ‘PATH’ environment variable.Setting Up the ...

Read More
Showing 5091–5100 of 5,881 articles
« Prev 1 508 509 510 511 512 589 Next »
Advertisements