Java

Java...


Java is an object oriented language but the most important thing about java is the same code can run anywhere! Java sits on a virtual machine (JVM) that is created for specific operating systems. So any java code can run on any machines JVM.

CREATED 2012-06-23 14:07:15.0

00-12-7C

UPDATED 2020-05-25 13:28:41.0

Constants


For the record....constants ... are not variables. They are constants i.e. they don't change. The very concept of a constant is that it doesn't change. A variable... on the other hand... changes... hence the word variable. It's value can change. So there is no such thing as a constant variable as I often hear whenever the topic comes up... just to be clear.

Now... in java... there are no constants. The concept is there but there is really no such thing. This is because a final is close enough to a constant. A final means that it is... well... final. The assigned value is not going to change.

But... there is a catch. In java a final doesn't necessarily mean that the value won't change... rather the pointer to that value won't change. In other words... the pointer to that spot in memory will not change. Not the memory that it is pointing to just the place it is pointing. This means if a constant was an instance of a class... and the class was mutable... well... it wouldn't really be a constant would it. So... while it is possible to create a psuedo constant in java i.e. the concept is there... there really isn't a hard constant... not in java.

CREATED 2020-01-23 05:32:07.0

010-00-00-21

UPDATED 2020-01-23 05:47:42.0

Initialization Block


public class A{
   static{
      System.out.println("Static initialization block");
   }
   {
      System.out.println("Instance initialization block");
   }
   public A(){
      System.out.println("No arg constructor of class A");
   }
A a1 = new A();
A a2 = new A();
Static initialization block
Instance initialization block;
Instance initialization block;

An initialization block comes in two flavors, static and instance. A Static initialization block is executed once when the class is initialized while an instance initialization block is executed when an object (copy of the class) is instantiated.

Example:

When this code is compiled the initialization block is copied to every constructor by the compiler. Every time a new object is intantiated from this class instance initialization block will execute.

When a1 is instantiated both the static and instance initialization blocks will execute. When a2 is instantiated only the instance initialization block will execute.

CREATED 2017-05-08 00:14:31.0

00-29-E8

UPDATED 2020-01-23 05:47:06.0

DBID: db.wam

Page Server: Ruger

©2019 Leistware.com
        Hello anonymous