Final Methods


The final keyword prevents a method from being overridden in a subclass, and is
often used to enforce the API functionality of a method.


class SuperClass{
public final void showSample() {
System.out.println("One thing.");
}
}


class SubClass extends SuperClass{
public void showSample() { // Try to override the final
// superclass method
System.out.println("Another thing.");
}
}

Attempting to compile the preceding code

Final methods cannot be overridden




Final Arguments

Method arguments are the variable declarations that appear in between the parentheses
in a method declaration.

public Record getRecord(int fileNumber, final int recordNumber) {}


the variable recNumber is declared as final, which of course means it can't be modified within the method. In this case, "modified" means reassigning a new value to the variable. In other words, a final argument must keep the same value that the parameter had when it was passed into the method.


Abstract Methods

An abstract method is a method that's been declared (as abstract) but not implemented. In other words, the method contains no functional code.


You mark a method abstract when you want to force subclasses to provide the implementation

public abstract void showSample();


It is illegal to have even a single abstract method in a class that is not explicitly declared abstract!


public class IllegalClass{
public abstract void doIt();
}


IllegalClass.java:1: class IllegalClass must be declared
abstract.
It does not define void doIt() from class IllegalClass.
public class IllegalClass{
1 error


Any class that extends an abstract class must implement all abstract methods
of the superclass, unless the subclass is also abstract


Look for concrete classes that don't provide method implementations for
abstract methods of the superclass. The following code won't compile:


public abstract class A {
abstract void foo();
}
class B extends A {
void foo(int I) { }
}


Synchronized Methods

The synchronized keyword indicates that a method can be accessed by only one
thread at a time


synchronized modifier can be applied only to methods—not variables, not classes, just methods. A typical synchronized declaration looks like this:

public synchronized Record retrieveUserInfo(int id) { }


Native Methods

The native modifier indicates that a method is implemented in platform-dependent
code, often in C.

Methods with Variable Argument Lists (var-args)

rules for var-args:




Var-arg type When you declare a var-arg parameter, you must specify the type of the argument(s) this parameter of your method can receive. (This can be a primitive type or an object type.)
Basic syntax To declare a method using a var-arg parameter, you follow the type with an ellipsis (...), a space, and then the name of the array that will hold the parameters received.
Other parameters It's legal to have other parameters in a method that uses a var-arg.
Var-args limits The var-arg must be the last parameter in the method's signature, and you can have only one var-arg in a method.






eg:


Legal:
void doStuff(int... x) { } // expects from 0 to many ints
// as parameters
void doStuff2(char c, int... x) { } // expects first a char,
// then 0 to many ints
void doStuff3(Animal... animal) { } // 0 to many Animals



Final Variables

Declaring a variable with the final keyword makes it impossible to reinitialize that
variable once it has been initialized with an explicit value


For primitives, this means that once the variable is assigned a value, the value can't be altered.








Transient Variables


If you mark an instance variable as transient, you're telling the JVM to skip
(ignore) this variable when you attempt to serialize the object containing it.





Volatile Variables


The volatile modifier tells the JVM that a thread accessing the variable must
always reconcile its own private copy of the variable with the master copy in
memory.



volatile modifier, to make your data thread-safe.



Static Variables and Methods


The static modifier is used to create variables and methods that will exist
independently of any instances created for the class. All static members exist
before you ever make a new instance of a class, and there will be only one copy of
a static member regardless of the number of instances of that class. In other words,
all instances of a given class share the same value for any given static variable.



Things you can mark as static:
■ Methods
■ Variables
■ A class nested within another class, but not within a method
■ Initialization blocks








Things you can't mark as static:
■ Constructors (makes no sense; a constructor is used only to create instances)
■ Classes (unless they are nested)
■ Interfaces
■ Method local inner classes (we'll explore this in Chapter 8)
■ Inner class methods and instance variables
■ Local variables



Declaring Enums

As of 5.0, Java lets you restrict a variable to having one of only a few pre-defined
values—in other words, one value from an enumerated list. (The items in the
enumerated list are called, surprisingly, enums.)

Declaring an enum outside a class:


enum CoffeeSize { BIG, HUGE, OVERWHELMING } // this cannot be
// private or protected
class Coffee {
CoffeeSize size;
}
public class CoffeeTest1 {
public static void main(String[] args) {
Coffee drink = new Coffee();
drink.size = CoffeeSize.BIG; // enum outside class
}
}


Here's an example of declaring an enum inside a class:

class Coffee2 {
enum CoffeeSize {BIG, HUGE, OVERWHELMING }
CoffeeSize size;
}
public class CoffeeTest2 {
public static void main(String[] args) {
Coffee2 drink = new Coffee2();
drink.size = Coffee2.CoffeeSize.BIG; // enclosing class
// name required
}
}


The following is NOT legal:

public class CoffeeTest1 {
public static void main(String[] args) {
enum CoffeeSize { BIG, HUGE, OVERWHELMING } // WRONG! Cannot
// declare enums
// in methods
Coffee drink = new Coffee();
drink.size = CoffeeSize.BIG;
}
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -