Articles on Trending Technologies

Technical articles with clear explanations and examples

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

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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 are the modifiers allowed to use along with local variables in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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

What is blank final variable? What are Static blank final variables in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

Static variables − Static variables are also known as class variables. You can declare a variable static using the keyword. Once you declare a variable static there would only be one copy of it in the class, regardless of how many objects are created from it.public static int num = 39;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.You must access instance variables using an ...

Read More

How to call another enum value in an enum's constructor using java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ 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 }Methods and variables in an enumerationEnumerations are similar to classes and, you can have variables, methods (Only concrete methods) and constructors within them.For suppose we have elements in an enumeration with values as −enum Scoters {    ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), ...

Read More

Does static factory method internally use new keyword to create object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 259 Views

Factory pattern is a design pattern (creational pattern) which is used to create multiple objects based on the data we provide. In it we create an object abstracting the process of creation.ExampleBelow given is the example implementation of the factory pattern. Here, we have an interface with name Employee and 3 classes: Student, Lecturer, NonTeachingStaff, implementing it. We have created a factory class (EmployeeFactory) with a method named getEmployee(). This method accepts a String value and returns an object of one of the classes, based on the given String value.import java.util.Scanner; interface Person{    void dsplay(); } class Student implements ...

Read More

How static class Object is created without reference of outer class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 1K+ Views

A static member (method/variable) belongs to the class and it will be loaded into the memory along with the class. You can invoke it without creating an object. (using the class name as reference). There is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.Examplepublic class Sample{    static int num = 50;    public static void demo(){       System.out.println("Value of num in the demo method "+ Sample.num);    } } public class ...

Read More

Why static methods of parent class gets hidden in child class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

When we have two classes where, one extends another and if, these two classes have same method including parameters and return type (say, sample) the method in the sub class overrides the method in the super class.i.e. Since it is inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.But if you call the method (sample), the sample method of the subclass will be executed overriding the super class’s method.Exampleclass Super{    public static void sample(){       System.out.println("Method of ...

Read More

How to call a non-static method of an abstract class from a static method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 9K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Hence, if you want to prevent instantiation of a class directly, you can declare it abstract.Accessing non-static methods of an abstract classSince you cannot instantiate an abstract ...

Read More

Can we make static reference to non-static fields in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 12K+ Views

A class in Java will have three kinds of variables namely, static (class), instance and, local.Local variables − These variables belong to and declared/defined within the methods/blocks/constructors. The scope of these variables lies within the method (or, block or, constructor) and will be destroyed after he execution of it.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.You must access instance variables using an object. i.e. ...

Read More
Showing 52431–52440 of 61,297 articles
Advertisements