Static Blocks in Java


The static blocks are anonymous blocks of code that can be written while defining classes. They get executed when the Class template is loaded in the memory by JVM. Please note that they get loaded while the Class template is being loaded and not when a new object is being created. So there is every chance that the static block will get executed only one during the entire runtime of a program.

This fact makes static blocks a good place to write some initialization stuff. The above example show how it is done.

One More Point: I found that , Just declaring a class variable does now load it in memory (Yes, the declaration got executed but against my expectations the static block was not executed.). The class template gets loaded only when we create a new object. And yes it stays loaded. Hence, further object creations will not invoke the static block.



EXAMPLE CODE:-


Class A: The class where we have written the static block.
Class B:- A Class where we have referenced ClassA, so that its template gets loaded in the memory.
Launcher_Class:- Its sole purpose is to house the main function and trigger the whole scenario by instantiating ClassB.

------------------CODE BEGINS------------------------

package testing_static_blocks;

public class ClassA
{

static{

System.out.println("Here I Load !!!!!!!");
}

}

------------------------------------------------------------------------------------
package testing_static_blocks;

public class ClassB
{

int x;
char y;
public ClassB()
{
new ClassA();
}



public void some_method()
{
x=10;
y='c';

}
}

------------------------------------------------------------------------------------


package testing_static_blocks;

public class Launcher_Class
{

public static void main(String[] args)
{

new ClassB();
}

}
----------------CODE ENDS-------------------------------



Til Next Time
Adios.

Types of CSS in HTML


Example of different types of CSS  in html:-

1) Inline:-

<p style="color:blue; font-family: Arial,Helvetica,Sans-serif; font-size: 14px;">Hi there! I'm styled with an INLINE style</p>

2) Internal Style Sheet:-


<head>
<style type="text/css">
<!--
.red {
color: #CE1A30;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
}
-->
</style>
</head>

<body>
...
<p class="red">Hi there! I'm styled with an embedded, or INTERNAL style sheet!</p>
</body>

3)
External or Attached Style SHeet:-

<head>
link href="StylePlacesCSS.css" rel="stylesheet" type="text/css" />
</head>

<p class="greenText">Oh hello! I'm styled with an EXTERNAL, or attached style sheet!</p>



NOTE:- Use of inline CSS:- interesting

Although inline styles are part of CSS, they are used infrequently. They present many
of the same problems as the older <font> tags in HTML. They only apply to one tag
at a time and are not easily reusable. So when are they used? Inline styles are useful
when an internal or external style sheet may not be available; a good example of this is
HTML-based e-mail. They are also used in certain situations to override other styles.

---------------------------


Till Next time,
Adios