6

Is it important to save all class code as individual .java files like following way?

Outer.java,
Inner.java,
Test.java

Or can I make a single java file as Test.java. Please explain anonymous class, how can we create anonymous class in java, and what is the advantage/disadvantage over normal class?

class Outer {
    private int data = 50;

    class Inner {
        void msg() {
            System.out.println("Data is: " + data);
        }
    }
}

class Test {
    public static void main(String args[]) {
        Outer obj = new Outer();
        Outer.Inner in = obj.new Inner();
        in.msg();
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Tilak Raj
  • 472
  • 4
  • 12
  • 2
    For this small code, you don't have to. Keep in mind that it's not recommended to have unrelated classes in one file.. – Maroun Jan 28 '14 at 06:21
  • 3
    Make Test public... Its always better to make the class that contains main() public. You can have only one "public" class in your .java file.. You can have 1 public class + any number of non-public classes in the same java file.. – TheLostMind Jan 28 '14 at 06:23
  • [Anonymous Class](https://www.google.co.in/search?client=ubuntu&channel=fs&q=anonymous+classes+in+java&ie=utf-8&oe=utf-8&gws_rd=cr&ei=-E7nUtXXIIWfiAe2lYCYBg) , [Some more discussion](http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java) – Suvarna Pattayil Jan 28 '14 at 06:33
  • I never understood why people would want to put different classes in the same file. Why do you want that? What advantages does that gives? Is there a negative effect if each class is in 1 seperate file? – roel Jan 28 '14 at 09:32

3 Answers3

4

See the Code Conventions for the Java programming language:

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

I think this should answer your question. As I stated in my comment, it's not recommended to write a file that contains several unrelated classes. However, if you have an associated classes with that one public class you have in the file, you can place them in the same source file.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

An anonymous class in Java is a class not given a name and is both declared and instantiated in a single statement. You should consider using an anonymous class whenever you need to create a class that will be instantiated only once.

Although an anonymous class can be complex, the syntax of anonymous class declarations makes them most suitable for small classes that have just a few simple methods.

An anonymous class must always implement an interface or extend an abstract class. However, you don’t use the extends or implements keyword to create an anonymous class. Instead, you use the following syntax to declare and instantiate an anonymous class:

new interface-or-class-name() { class-body }

Within the class body, you must provide an implementation for each abstract method defined by the interface or abstract class. Here’s an example that implements an interface named runnable, which defines a single method named run:

runnable r = new runnable()
    {
        public void run()
        {
            //code for the run method goes here
        }
    };

Here are a few other important facts concerning anonymous classes:

  1. An anonymous class cannot have a constructor. Thus, you cannot pass parameters to an anonymous class when you instantiate it.

  2. An anonymous class can access any variables visible to the block within which the anonymous class is declared, including local variables.

  3. An anonymous class can also access methods of the class that contains it.

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
learner
  • 365
  • 1
  • 3
  • 16
1

yes you can do this , but it should be in small programming, while in big application you have to make class individual, Java strongly recommended that to make class different file, but it depends on you to do so.