Wednesday, March 12, 2014

Importing Other Classes

Importing Other Classes
ava provides the keyword import to indicate that you want to use a
package or a class from a package. For example, to use the java.io.File class,
you must have the following import statement:
import java.io.File;

public class Demo {
    //...
}

import statements
  1. 'import' statements must come after the package statement but before the class declaration.
  2. The import keyword can appear multiple times in a class.
import java.io.File;
import java.util.List;

public class Demo {

    //...
}
import all classes
You can import all classes in the same package by using the wild character
*. For example, the following code imports all members of the java.io package.
import java.io.*;

public class Demo {
    //...
}
However, to make your code more readable, it is recommended
that you import a package member one at a time.
import java.io.File;
import java.io.FileReader;

fully qualified name
Members of the java.lang package are imported automatically. Thus,
to use the java.lang.String, for example, you do not need to explicitly import the class.
The only way to use classes that belong to other packages without
importing them is to use the fully qualified names of the classes in your code. For example,
the following code declares the java.io.File class using its fully qualified name.
java.io.File file = new java.io.File(filename);
If you class import identically-named classes from different packages,
you must use the fully qualified names when declaring the classes.
For example, the Java core libraries contain the classes java.sql.
Date and java.util.Date. In this case, you must write the fully qualified names of java.sql.
Date and java.util.Date to use them.


http://www.java2s.com/Tutorial/Java/0100
__Class-Definition/Catalog0100__Class-Definition.htm





 










































»»  READMORE...

Friday, June 14, 2013

satio

joeyyyyy
»»  READMORE...

Monday, December 19, 2011

Class Definition » toString

Override toString() for Box class.
class Box {
  double width;

  double height;

  double depth;

  Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
  }

  public String toString() {
    return "Dimensions are " + width + " by " + depth + " by " + height + ".";
  }
}

class toStringDemo {
  public static void main(String args[]) {
    Box b = new Box(101214);
    String s = "Box b: " + b; // concatenate Box object

    System.out.println(b)// convert Box to string
    System.out.println(s);
  }
}
 
 
override the toString method in your classes

public class MainClass {
  public static void main(String[] args) {
    Employee emp = new Employee("Martinez""Anthony");
    System.out.println(emp.toString());
  }
}

class Employee {
  private String lastName;

  private String firstName;

  public Employee(String lastName, String firstName) {
    this.lastName = lastName;
    this.firstName = firstName;

  }

  public String toString() {
    return "Employee[" this.firstName + " " this.lastName + "]";
  }
}
 
 
Use Reflection To build toString method

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Main {

  private Integer id;

  private String name;

  private String description;

  public static final String KEY = "APP-KEY";

  private transient String secretKey;

  public Main(Integer id, String name, String description, String secretKey) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.secretKey = secretKey;
  }

  public String toString() {
    return ReflectionToStringBuilder.toString(this, ToStringStyle.
SIMPLE_STYLE, true, true);
  }

  public static void main(String[] args) {
    Main demo = new Main (1"A""B""C");
    System.out.println("Demo = " + demo);
  }
}
 
 
Reflection based toString() utilities

import java.lang.reflect.Field;

public class Main {
  String hello = "world";

  int i = 42;

  public static void main(String args[]) {
    System.out.println(Util.toString(new MyClass()));
    
    System.out.println(Util.toString(new MyAnotherClass()));
  }
}

class Util {
  public static String toString(Object o) {
    StringBuilder sb = new StringBuilder();
    toString(o, o.getClass(), sb);
    return o.getClass().getName()"\n"+sb.toString();
  }

  private static void toString(Object o, Class clazz, StringBuilder sb) {
    Field f[] = clazz.getDeclaredFields();

    for (int i = 0; i < f.length; i++) {
      f[i].setAccessible(true);
      try {
        sb.append(f[i].getName() "=" + f[i].get(o)+"\n");
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (clazz.getSuperclass() != null)
      toString(o, clazz.getSuperclass(), sb);
  }
}

class MyClass {
  int i = 1;

  private double d = 3.14;
}

class MyAnotherClass extends MyClass{
  int f = 9;
}
 
 
Use a generic toString()

import java.lang.reflect.Field;

public class Main {
  public static void main(String args[]) {
    System.out.println(new MyClass().toString());

  }
}

class MyClass {
  String hello = "hi";

  int i = 0;

  public String toString() {
    StringBuilder sb = new StringBuilder();
    Class cls = getClass();
    Field[] f = cls.getDeclaredFields();

    for (int i = 0; i < f.length; i++) {
      f[i].setAccessible(true);
      try {
        sb.append(f[i].getName()+"="+ f[i].get(this)+"\n");
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    if (cls.getSuperclass().getSuperclass() != null) {
      sb.append("super:"super.toString()+"\n");
    }
    return cls.getName()+"\n" + sb.toString();
  }
}
 
 
Jakarta Commons toString Builder

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Main {
  private String id;

  private String firstName;

  private String lastName;

  public Main() {
  }

  public String toString() {
    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).
append("id", id).append(
        "firstName", firstName).append("lastName", lastName).toString();
  }

  public static void main(String[] args) {
    Main example = new Main();
    example.id = "1";
    example.firstName = "First Name";
    example.lastName = "Last Name";

    System.out.println("example = " + example);
  }
}
 
http://www.java2s.com/Tutorial/Java/0100
__Class-Definition/Catalog0100__Class-Definition.htm
 
»»  READMORE...

Friday, December 16, 2011

Class Definition » Static Import

Static import

In Java 5 you can import static fields using the import static keywords.
Before Java 5, to use a static final field in the Calendar class, you must first import the Calendar class.
import java.util.Calendar;
Then, you can use it by using the notation className.staticField.
if (today == Calendar.SATURDAY)
In Java 5, you can do
import static java.util.Calendar.SATURDAY;

if (today == SATURDAY)
 
 
Static importing the Math Class Methods

import static java.lang.Math.sqrt;

public class MainClass {

  public static void main(String[] arg) {
    System.out.println(sqrt(2));
  }

}
1.4142135623730951
 
 
Use static import to bring sqrt() and pow() into view.

import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

class Hypot {
  public static void main(String args[]) {
    double side1, side2;
    double hypot;

    side1 = 3.0;
    side2 = 4.0;

    hypot = sqrt(pow(side1, 2+ pow(side2, 2));

    System.out.println("Given sides of lengths " + side1 + " and " 
 + side2 + " the hypotenuse is "
        + hypot);
  }
}
 
http://www.java2s.com/Tutorial/Java/0100
__Class-Definition/Catalog0100__Class-Definition.htm
 
»»  READMORE...

Thursday, December 15, 2011

Class Definition » import

Importing Other Classes
ava provides the keyword import to indicate that you want to use a
package or a class from a package. For example, to use the java.io.File class,
you must have the following import statement:
import java.io.File;

public class Demo {
    //...
}

import statements
  1. 'import' statements must come after the package statement but before the class declaration.
  2. The import keyword can appear multiple times in a class.
import java.io.File;
import java.util.List;

public class Demo {

    //...
}
 
 
import all classes
You can import all classes in the same package by using the wild character
*. For example, the following code imports all members of the java.io package.
import java.io.*;

public class Demo {
    //...
}
However, to make your code more readable, it is recommended
that you import a package member one at a time.
import java.io.File;
import java.io.FileReader;
 
 
 

fully qualified name
Members of the java.lang package are imported automatically. Thus,
to use the java.lang.String, for example, you do not need to explicitly import the class.
The only way to use classes that belong to other packages without
importing them is to use the fully qualified names of the classes in your code. For example,
the following code declares the java.io.File class using its fully qualified name.
java.io.File file = new java.io.File(filename);
If you class import identically-named classes from different packages,
you must use the fully qualified names when declaring the classes.
For example, the Java core libraries contain the classes java.sql.
Date and java.util.Date. In this case, you must write the fully qualified names of java.sql.
Date and java.util.Date to use them.


http://www.java2s.com/Tutorial/Java/0100
__Class-Definition/Catalog0100__Class-Definition.htm





 









































»»  READMORE...

Monday, December 12, 2011

Class Definition » Abstract Class

Abstract Classes
  1. Provide a contract between a service provider and its clients.
  2. An abstract class can provide implementation.
  3. To make an abstract method, use the abstract modifier in front of the method declaration.
public abstract class DefaultPrinter {
  public String toString() {
    return "Use this to print documents.";
  }

  public abstract void print(Object document);
}
  1. The toString method has an implementation, so you do not need to override this method.
  2. The print method is declared abstract and does not have a body.
public class MyPrinter extends DefaultPrinter {
  public void print(Object document) {
    System.out.println("Printing document");
    // some code here
  }
}
 
A demonstration of abstract.
abstract class {
  abstract void callme();

  void callmetoo() {
    System.out.println("This is a concrete method.");
  }
}

class extends {
  void callme() {
    System.out.println("B's implementation of callme.");
  }
}

class AbstractDemo {
  public static void main(String args[]) {
    B b = new B();

    b.callme();
    b.callmetoo();
  }
}
 
Using abstract methods and classes.
abstract class Figure {
  double dim1;

  double dim2;

  Figure(double a, double b) {
    dim1 = a;
    dim2 = b;
  }

  abstract double area();
}

class Rectangle extends Figure {
  Rectangle(double a, double b) {
    super(a, b);
  }

  double area() {
    System.out.println("Inside Area for Rectangle.");
    return dim1 * dim2;
  }
}

class Triangle extends Figure {
  Triangle(double a, double b) {
    super(a, b);
  }

  double area() {
    System.out.println("Inside Area for Triangle.");
    return dim1 * dim2 / 2;
  }
}

class AbstractAreas {
  public static void main(String args[]) {
    Rectangle r = new Rectangle(95);
    Triangle t = new Triangle(108);
    Figure figref;

    figref = r;
    System.out.println("Area is " + figref.area());

    figref = t;
    System.out.println("Area is " + figref.area());
  }
}
 
 
http://www.java2s.com/Tutorial/Java/0100
__Class-Definition/Catalog0100__Class-Definition.htm
»»  READMORE...

Sunday, December 11, 2011

Class Definition » Final Class

Final Classes
You can prevent others from extending your class by making it final using the keyword final in the class declaration.
final class FinalClass{


}
 
Using final to Prevent Overriding
class {
  final void meth() {
    System.out.println("This is a final method.");
  }
}
   
class extends {
  void meth() { // ERROR! Can't override.
    System.out.println("Illegal!");
  }
}
 
http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Catalog0100__Class-Definition.htm
»»  READMORE...