Saturday, December 10, 2016

Can a class be declared static ?

abstract static class OutsideClass {

static void m1(){
System.out.println("m1 method");
}

public static void main(String args[]){
OutsideClass.m1();
}
}

Answer : 

No because all classes are already static by nature because anyway a class exists only one time

Friday, December 9, 2016

Read file from command prompt

C:\Program Files\Java\javapractise>type Outer.*

how to delete files in command prompt



1) go to your directory , say c:\program files\practise
2) c:\program files\practise > del *.class

Difference between compilation and runtime

class Outer {
static void m3(){
System.out.println("????????????????? " +x);
x = 20;
}
static void m4(){
System.out.println("How are you ? are you Good ?ddddddddddddddd " +x);
}
static {
System.out.println("Hi Balaji................  ?aaaaaaaaaaaaaaa ");
m3();
}
static {
System.out.println("Hi Balaji................  ?bbbbbbbbbbbbbbb ");
m4();
}
static int x = 10;
static int y = x;
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ? " + y);
}
}
******************************************************************************************************************************************************************
//Execute this and see
Run using 
javac Outer.java
java Outer$Inner

*********************************************************************************

In this in place of  x in System.out.println("????????????????? " +x);
if you declare it as m ,
System.out.println("????????????????? " +m);
then you will get comiplation error because the jvm sees whether the code is syntacally right or not, not its correctness..

Similarly you can change the method m3() as say m3333() and you will see that you will get
compile error...

Whereas runtime exception or error is checking for the correctness of the code when the program actually runs
*********************************************************************************

Inner class - Example2

class Outer {
/*static class Inner {
static void m1(){
System.out.println("Hi I am Inside m1() method");
}
public void m2(){
System.out.println("Will this static and non static work without main() method ?");
}
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ?");
new Inner().m2();
Inner.m1();
}
}
public static void main(String args[]) {
Inner inner = new Inner();
}*/

static void m3(){
System.out.println("How are you ? are you Good ?ccccccccccccccc " +x);
x = 20;
}
static void m4(){
System.out.println("How are you ? are you Good ?ddddddddddddddd " +x);
}
static {
System.out.println("Hi Balaji................  ?aaaaaaaaaaaaaaa ");
m3();
}
static {
System.out.println("Hi Balaji................  ?bbbbbbbbbbbbbbb ");
m4();
}
static int x = 10;
static int y = x;
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ? " + y);
}
}

//Execute this and see
Run using
javac Outer.java
java Outer$Inner

Inner class - Example1

class Outer {
static class Inner {
static void m1(){
System.out.println("Hi I am Inside m1() method");
}
public void m2(){
System.out.println("Will this static and non static work without main() method ?");
}
public static void main(String args[]) {
System.out.println("Hi Balaji how are you ?");
new Inner().m2();
Inner.m1();
}
}

public static void main(String args[]) {
Inner inner = new Inner();
}
}

//Execute this and see
Run using
javac Outer.java
java Outer$Inner