CoreJava#1 BlocksRAGHUSIR
CoreJava#1 BlocksRAGHUSIR
1|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
static {
System.out.println("Welcome to NareshIt Technologies ");
}
2|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
Example#2
public class Student{
{
System.out.println("Welcome to RAGHU sir Class ");
}
Example#3
public class Person {
static {
System.out.println("Welcome to NareshIt Technologies ");
}
{
System.out.println("Welcome to Raghu Sir class");
}
Example#4
public class Product {
static {
System.out.println(" You Are In ");
}
static {
System.out.println(" NareshIt Techonolgies ");
}
static {
System.out.println(" All the best ");
}
public static void main(String[] args) {
System.out.println(" Welcome to Java class ");
}
}
Output:______________
Example#5
public class Employee {
{
System.out.println(" Learn Java ");
}
{
System.out.println(" From Raghu Sir ");
}
{
System.out.println(" Get Job at Software Company ");
}
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
}
4|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
}
Output:_______________
Example#6
public class Course {
static {
System.out.println(" Hello Student! ");
}
{
System.out.println(" Learn Java Course ");
}
{
System.out.println(" From Raghu Sir. ");
}
static {
System.out.println(" Get Job at Software Company. ");
}
}
Output:____________
Example#7
public class Sample {
{
System.out.println(" Learn Java Course ");
}
public static void main(String[] args) {
new Sample() {
5|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
{
System.out.println(" From Raghu Sir.");
}
};
}
static {
System.out.println(" NareshIttechnologies, Ameerpet ");
}
}
Output:____________
Example#8
public class Contract {
public static void main(String[] args) {
new Contract () {
{
System.out.println(" From Raghu Sir.");
}
static {
System.out.println(" Hi User !!");
}
};
}
}
Output:____________
Example#9
public class Raghu{
{
System.out.println(" Welcome to");
}
public static void main(String[] args) {
6|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
new Raghu() {
{
System.out.println(" From Raghu Sir.");
}
{
System.out.println(" All the best!! ");
}
};
}
{
System.out.println(" Java Class ");
}
}
Example#10
public class Admin {
static {
System.out.println(" From Raghu Sir! ");
}
{
System.out.println(" Welcome to All");
}
public static void main(String[] args) throws Exception {
Class.forName("Admin");
}
static {
System.out.println(" Java course!! ");
}
{
System.out.println(" All the best!! ");
}
7|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
8|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
9|Page
-by RAGHU SIR [NARESH IT, HYDERABAD, P: 040-2374 6666/ 9000994007 /08]
Exercise # Programming
1. Write class ‘A’ with one static block and print hello message
2. Write class ‘B’ with instance block and print welcome message
3. Write a java class ‘A’ with static block that prints NareshItTech 10
times
4. Write class ‘D’ with one static block(print hello) and one instance
block (print Hi)
6. Write java class ‘A’ having 2 static block prints ‘hi’,’welcome’ and one
instance block prints ‘bye’
7. Create A class with m1() method that prints ‘Hello M1’. Create A
class object in B class and call A class m1() method in static block of
B class.
9. Create class Connection with get() method which print ‘Oracle Conn’.
Create object of Connection class in DbOperation class and call get()
method using static block.
10. Create Admin class with show() static method. Call show()
method in Admin static block.
FB: https://www.facebook.com/groups/thejavatemple/
email: javabyraghu@gmail.com
10 | P a g e