public class InnerClassTest {
public static void main(String args[]) {
//creating local inner class inside method
class Local {
public void name() {
System.out.println("Example of Local class in Java");
}
}
//creating instance of local inner class
Local local = new Local();
local.name(); //calling method from local inner class
//Creating anonymous inner class in java for implementing thread
Thread anonymous = new Thread(){
@Override
public void run(){
System.out.println("Anonymous class example in java");
}
};
anonymous.start();
//example of creating instance of inner class
InnerClassTest test = new InnerClassTest();
InnerClassTest.Inner inner = test.new Inner();
inner.name(); //calling method of inner class
}
/*
* Creating Inner class in Java
*/
private class Inner{
public void name(){
System.out.println("Inner class example in java");
}
}
}
t
Showing posts with label Core. Show all posts
Showing posts with label Core. Show all posts
Saturday, September 27, 2014
Service Locator core
package ServiceLocater;
import java.util.ServiceLoader;
public abstract class HelloProvider {
public static HelloProvider getDefault() {
ServiceLoader<HelloProvider> ldr = ServiceLoader.load(HelloProvider.class);
for (HelloProvider provider : ldr) {
//We are only expecting one
return provider;
}
throw new Error ("No HelloProvider registered");
}
public abstract String getMessage();
public static void main(String[] ignored) {
HelloProvider provider = HelloProvider.getDefault();
System.out.println(provider.getMessage());
}
}
class HelloImpl extends HelloProvider {
@Override
public String getMessage() {
return "Hello World";
}
}
import java.util.ServiceLoader;
public abstract class HelloProvider {
public static HelloProvider getDefault() {
ServiceLoader<HelloProvider> ldr = ServiceLoader.load(HelloProvider.class);
for (HelloProvider provider : ldr) {
//We are only expecting one
return provider;
}
throw new Error ("No HelloProvider registered");
}
public abstract String getMessage();
public static void main(String[] ignored) {
HelloProvider provider = HelloProvider.getDefault();
System.out.println(provider.getMessage());
}
}
class HelloImpl extends HelloProvider {
@Override
public String getMessage() {
return "Hello World";
}
}
Generics Sample 1
public class Generics1<A> {
private A a;
public void set(A a) {
this.a = a;
}
public A get() {
return a;
}
public <U extends Number> void inspect(U u) {
System.out.println("T: " + a.getClass().getName());
System.out.println("U: " + a.getClass().getName());
}
public static void main(String[] args) {
Generics1<Integer> integerBox = new Generics1<Integer>();
integerBox.set(new Integer(10));
integerBox.inspect(10);
}
}
private A a;
public void set(A a) {
this.a = a;
}
public A get() {
return a;
}
public <U extends Number> void inspect(U u) {
System.out.println("T: " + a.getClass().getName());
System.out.println("U: " + a.getClass().getName());
}
public static void main(String[] args) {
Generics1<Integer> integerBox = new Generics1<Integer>();
integerBox.set(new Integer(10));
integerBox.inspect(10);
}
}
Generics
public class Genericfactory {
static <T> void printClassnName(T anyType) {
System.out.println(anyType.getClass().getPackage());
System.out.println(anyType.getClass().getName());
}
static void printClassnNameTest(Object test) {
System.out.println(test.getClass().getPackage());
System.out.println(test.getClass().getName());
}
public static void main(String[] args) {
Genericfactory.printClassnName(String.class);
Genericfactory.printClassnName(new String(""));
Genericfactory.printClassnName(Integer.class);
Genericfactory.printClassnName(new Integer(3));
Genericfactory.printClassnName(Genericfactory.class);
Class test= Genericfactory.class;
Genericfactory.printClassnNameTest(test);
}
}
Enum
public class EnumTest {
State test;
public void setCurrentState(State currentState) {
test = currentState;
}
public State getCurrentState() {
return test;
}
public static void main(String[] args) {
System.out.println(State.FrameworkInatilize);
System.out.println(State.test);
EnumTest test=new EnumTest();
test.setCurrentState(State.Notify);
test. stateSwitch();
}
public void stateSwitch() {
switch (getCurrentState()) {
case Notify:
System.out.println("Notify");
System.out.println( test.isNotify());
break;
default:
break;
}
}
}
ComparatorComparable
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ComparatorComparableExample {
public static void main(String[] args){
Student s1 = new Student(10, "john", 500.50f);
Student s2 = new Student(1, "Abc", 700);
Student s3 = new Student(7, "xyz", 800);
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
MyClass m =new MyClass();
Collections.sort(al,m);
for(Object o: al){
Student s = (Student)o;
s.display();
}
}
static class Student /*implements Comparable */{
Student(int id, String name, float salary){
this.id = id;
this.name =name;
this.salary = salary;
}
void display(){
System.out.println(id + ":" + name + ":" + salary);
}
int id;
String name;
float salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
/*
@Override
public int compareTo(Object o) {
Integer i1 = id;
Student i2 = (Student) o;
return i1.compareTo(i2.getId());
}
*/
}
static class MyClass implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return ((Integer)s1.getId()).compareTo(s2.getId());
}
}
}
import java.util.Collections;
import java.util.Comparator;
public class ComparatorComparableExample {
public static void main(String[] args){
Student s1 = new Student(10, "john", 500.50f);
Student s2 = new Student(1, "Abc", 700);
Student s3 = new Student(7, "xyz", 800);
ArrayList al = new ArrayList();
al.add(s1);
al.add(s2);
al.add(s3);
MyClass m =new MyClass();
Collections.sort(al,m);
for(Object o: al){
Student s = (Student)o;
s.display();
}
}
static class Student /*implements Comparable */{
Student(int id, String name, float salary){
this.id = id;
this.name =name;
this.salary = salary;
}
void display(){
System.out.println(id + ":" + name + ":" + salary);
}
int id;
String name;
float salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
/*
@Override
public int compareTo(Object o) {
Integer i1 = id;
Student i2 = (Student) o;
return i1.compareTo(i2.getId());
}
*/
}
static class MyClass implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return ((Integer)s1.getId()).compareTo(s2.getId());
}
}
}
Subscribe to:
Posts (Atom)