JavaBeat
calling cards | international calling cards | phone card
Search JavaBeat

Certification Kits
SCJP 1.5 Exam Questions
SCJP 1.6 Exam Questions
SCWCD 5.0 Exam Questions
SCBCD 5.0 Exam Questions
SCJP 1.5 Links
SCJP 1.5 Home
SCJP Books
SCWCD Books
SCBCD Books
New to SCJP?
SCJP 1.4 or SCJP 1.5?
Objectives
Articles
Mock Exams
Tutorials
Forums
SCJP Exam Voucher
Certification Links
SCJP 1.4
SCJP 1.5
SCJP 1.6
SCWCD 1.4
SCWCD 5.0
SCBCD 5.0
SCEA
SCEA 5.0
JavaBeat
Home
Articles
Tips
Code
QnA
Forums
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
1. Arrays 2. Variables Declarations 3. Constructors 4. Flow Control
5. Assertions 6. Java Keywords 7. Interfaces 8. Package
9. Finalization 10. Gargage Collection-1 11. Exception 12. Gargage Collection-2
13. Polymorphism 14. Encapsulation 15. Passing variables 16. Equals Method
17. Operators 18. Literals 19. Wrapper 20. String and StringBuffer
21. Math class 22. Threads 23. Thread Methods 24. Thread States
25. Thread Synchronization 26. Collections
Package and class declarations


A package represents a group of classes. A package statement should be the first valid statement in the source file. If there is no package statement, the classes in the source file belong to the default unnamed package, or else they belong to the named package. Only one package statement is allowed in a source file.

In a source file, there can be only one public class, and the name of the file should match that of the class. An import statement allows you to use a class directly instead of referring to it using its fully qualified name. Import statements must come after any package statement and before the class declarations, as shown below:


package com.abc; // package statement
import java.net.*; // wild card import (imports all classes in the package)
import java.util.Vector; // class import (only the given class is imported)

Nested class declarations
A nested class or inner class is a class defined inside another class. Nested classes can be non-static, method-local, anonymous, or static.

Non-static inner classes
A non-static inner class definition does not use the static keyword (see Java keywords and identifiers for more information on keywords). It is defined within the scope of the enclosing class, as follows:


class MyClass
{
class MyInner{}
}

To instantiate a non-static inner class, you need an instance of the outer class.

A non-static inner class has access to all the members of the outer class. From inside the outer instance code, use the inner class name alone to instantiate it:


MyInner myInner = new MyInner();

From outside the outer instance code, the inner class name must be included in the outer instance:


MyClass myClass = new MyClass();
MyClass.MyInner inner = myClass.new MyInner();

Modifiers that can be applied to non-static inner classes are final, abstract, public, private, and protected.

Method local inner classes
A method local inner class can be instantiated in the method where it is defined. It can also be in a constructor, a local block, a static initializer, or an instance initializer. It cannot access the local variables of the enclosing method unless they are final. The only modifiers for such a class are abstract or final.

Anonymous inner classes
Anonymous inner classes are inner classes that have no name. They can extend a class or implement an interface. For instance:


button1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
button1_mouseClicked(e);
}
});

This example shows an anonymous inner class. It is important to note that the class has been defined within a method argument.

Static nested classes
A static nested class is a static member of the enclosing class. It does not have access to the instance variables and methods of the class. For instance:


class MyOuter
{
static class MyNested{}
}
class Test
{
public static void main(String args[]){
MyOuter.MyNested n = new MyOuter.MyNested();
}
}

As this example shows, a static nested class does not need an instance of the outer class for instantiation.

1. Arrays 2. Variables Declarations 3. Constructors 4. Flow Control
5. Assertions 6. Java Keywords 7. Interfaces 8. Package
9. Finalization 10. Gargage Collection-1 11. Exception 12. Gargage Collection-2
13. Polymorphism 14. Encapsulation 15. Passing variables 16. Equals Method
17. Operators 18. Literals 19. Wrapper 20. String and StringBuffer
21. Math class 22. Threads 23. Thread Methods 24. Thread States
25. Thread Synchronization 26. Collections
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details

Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat