Home > JAVA > Class

Class

Source file declaration rules:

  1. There can be only one public class per source code file. If there is a public class, the name of the file must match the name of the public class. If there is no a public class, the file can be have a name different with a class name.
  2. Comments can appear at the beginning or end of any line in the source code file.
  3. If the class is part of a package, the package statement must be the first line in the source code file.
  4. If there are import statements, they must go between the package statement and the class declaration.
  5. import and package statements apply to all classes within a source file. There’s no way to declare multiple classes in a file and have them in different packages, or use different imports.
  6. A file can have more than one nonpublic class.

 

Class declaration:
[modifier] class [class_name] { }

 

Class modifier divided into:

  1. Access modifier: public or default.
  2. Non-access modifiers: strictfp, final and abstract.

 

Class naming standard.

  1. The first letter should be capitalized.
  2. “camelCase”

Example: PrintWriter, BigDecimal.

 

Class Access

  1. Access mean visibility. Class A has access to class B:
  2. Can create instance of class B.
  3. Can extend class B.
  4. Can access certain methods and variables within class B (depending on the access control of those methods and variables).

 

Default Access
A class with default access has no modifier preceding it in the declaration. We can call default aceess as package-level access. Because a class with default access can be seen only by classes within the same package.
Class declaration:
class [class_name] { }

 

Public Access
Class with public access can be seen by all class.
Class declaration:
public [class_name] { }

 

Other (Nonaccess) Class Modifier
stricfp
Marking a class as strictfp means that any method code in the class will conform to IEEE 754 standard rules for floating points.

final
The final keyword means the class can’t be subclassesd.

abstract
An abstract class can never be instantiated. Sample abstract class declaration:

public abstract class BaseObject {

private String id;

public abstract String toString();

public abstract boolean equals(Object o);

public abstract int hashCode();

}

Categories: JAVA
  1. No comments yet.
  1. July 1, 2014 at 2:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: