Java Articles

Page 321 of 450

What is a variable, field, property in Java?

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

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

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

ArrayIndexOutOfBounds Vs ArrayStoreException in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Aug-2019 447 Views

An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.Creating an arrayIn Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −int myArray[] = new int[7]; ...

Read More

What are the drawbacks of the arrays in Java?

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

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.Element − Each item stored in an array is called an element.Index: Each location of an element in an array has a numerical index, which is used to identify the element.The size of the array will be determined at the time of creation.Disadvantages of arraysDeleting or inserting − You cannot insert a new element at the ...

Read More

How do you find continuous sub array whose sum is equal to a given number in Java?

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

To find continuous sub array whose sum is equal to a given number −Iterate through the array.At each element add the next n elements one by one, when the sum equals to the required value print the sub array.Exampleimport java.util.Arrays; import java.util.Scanner; public class sub_arrays {    public static void main(String args[]){       //Reading the array from the user       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created: ");       int size = sc.nextInt();       int[] myArray = new int[size];   ...

Read More
Showing 3201–3210 of 4,496 articles
« Prev 1 319 320 321 322 323 450 Next »
Advertisements