This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDriver { | |
public static void main(String[] args){ | |
System.out.println("Hello World!") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Bar { | |
public static void main(String[] args) { | |
Class<?> cl=new Object(){}.getClass().getEnclosingClass(); | |
System.out.println(cl==Bar.class); //will print true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomeClass { | |
public void foo(){ | |
class Local {}; | |
String name = Local.class.getEnclosingMethod().getName(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class SomeClass { | |
private static final Logger log = LoggerFactory.getLogger(new Object() { | |
}.getClass().getEnclosingClass()); | |
public void foo(){ | |
log.debug("foo()"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class SomeClass { | |
private static final class Local {}; | |
private static final Logger log = LoggerFactory.getLogger(Local.class.getEnclosingClass()); | |
public void foo(){ | |
log.debug("foo()"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class SomeClass { | |
private static final Logger log = LoggerFactory.getLogger(new Object() { | |
}.getClass().getEnclosingClass()); | |
public void foo(){ | |
class Local {}; | |
String name = Local.class.getEnclosingMethod().getName(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Horse extends Animal{ | |
@Override | |
public void sound(){ | |
System.out.println("Neigh"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Driver { | |
public static void main(String args[]){ | |
Animal obj = new Horse(); | |
obj.sound(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// changing the implementing object in run-time (normally done in compile time) | |
class C implements I { | |
private I i; | |
// forwarding | |
public C(I i){ this.i = i; } | |
public void f() { i.f(); } | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] arguments) { | |
C c = new C(new A()); | |
c.f(); // output: A: doing f() | |
c = new C(new B()); | |
c.f(); // output: B: doing f() | |
} | |
} |
OlderNewer