Downcasting In Java

java-thumbnail

Upcasting is assigning the sub class reference object to the parent class which is allowed in Java. Downcasting is assigning parent class reference object to the sub class which is not allowed in Java. However, if you do downcasting, there will not be any compiler error. But, there will be runtime exception java.lang.ClassCastException. Then why […]

Static Nested Class In Java

java-thumbnail

Static Nested classes are inner classes which are declared inside another class and also declared with static keyword. The main difference between inner class and static nested classes is that inner classes are non-static and static nested classes are static. Also known as static classes. Static nested classes can be accessed using the enclosing class […]

Static Initializer , Instance Initializer And Constructor In Java

java-thumbnail

This example show the order of the Java initialization blocks. Java has static initializer, instance initializer and constructor for initializing the variables. These blocks are used for the different purposes and leads to confusion among the programmers. Order of the execution of these blocks are as follows: All Static Initializers executed in the same order […]

Instance Initializer In Java

java-thumbnail

In my previous example I have explained how to use static initializer in Java.This example shows how to use the instance initializer block in Java. The following are the few features of instance blocks. Instance initializers are block of code with out any keyword keyword. The code which needs to be initialized will be put […]

Static Initializer In Java

java-thumbnail

This example shows how to use the static initializer in Java. The following are the few features of static blocks. Static initializers are block of code with static keyword. These are executed when the class is loaded or accessed first time by any application. Only static variables can be initialized inside static blocks. Instance variables […]

How To Get Classpath in Java

java-thumbnail

This example demonstrates how to get the classpath string using the Java program. It is simple that you just need to use the System.getProperty() method with the classpath variable name which will return the classpath string. Lets look at the example. Output This is the simple example to get the classpath configuration in your system […]

How To Set Default Locale In Java

java-thumbnail

Every system has its own default locale set in the native library. It is based on the current location or country of that operating system is accessed. Java has API to access the locales and display the text on appropriate languages. In some cases, the default locale may not be correct or it needs change […]

How To Get OS In Java

java-thumbnail

This post writes a simple Java example program ┬áto get the current operating system. There is a system property in Java,┬áSystem.getProperty(“os.name”) which returns the operating system. However, this utility returns the operating system type like Windows, Mac, Unix or Linux. Lets look at the below example. GetOSExample.java Output…

File Modified Date Manipulation in Java

java-thumbnail

This example demonstrates the basic data manipulation operation in a File object. Java File object has set of methods to perform the following operations: Get the file last modified date Change the file modified date Lets look at the example to see how the above operations are implemented in Java example program. We use SImpleDateFormat […]

How To Set The File Permissions In Java

java-thumbnail

Setting the file permissions in Java is OS specific. Each type of file system have different kind of file systems. Java provides the very generic solution to set the file permissions for all kind of file systems. This example demonstrates how to set file permissions to a file in the Linux operating system using Java […]