Java constructor in an anonymous class

How to declare a constructor inside of anonymous class?

send pies

posted 11 years ago

  • Report post to moderator
  • How to declare a constructor inside of anonymous class?

    Bartender

    send pies

    posted 11 years ago

    • 1
  • Report post to moderator
  • A constructor is declared using the class’s name. An anonymous class, by definition, does not have a name.

    What you can do, however, is declare an instance initializer, just like you might do in a named class.

    Marshal

    send pies

    posted 11 years ago

    • 1
  • Report post to moderator
  • You can’t declare a constructor for an anonymous class, because the name for a constructor must be the same as the name of the class. And since the class in question has no name, you can’t declare a constructor for the class.

    But let me answer the question which might have led to this question: how do I initialize the state of an object which is an instance of an anonymous class? And the answer is to use an instance initializer. Like this:

    send pies

    posted 11 years ago

  • Report post to moderator
  • Why do you need a constructor in your anonymous class anyways? Can’t you do your initialization outside your anonymous class?.

    send pies

    posted 11 years ago

  • Report post to moderator
  • Jayesh A Lalwani wrote: Why do you need a constructor in your anonymous class anyways? Can’t you do your initialization outside your anonymous class?.

    I dont need.
    Just had a question about the language.
    I’ve thought that it was not possible, and would just like a confirmation.

    send pies

    posted 11 years ago

    • 1
  • Report post to moderator
  • send pies

    posted 11 years ago

  • Report post to moderator
  • Jayesh A Lalwani wrote: Why do you need a constructor in your anonymous class anyways? Can’t you do your initialization outside your anonymous class?.

    Explaining how the question had been originated, I had the following situation:

    Class AA had two constructors, a default constructor and one with a String argument.

    Creating an anonymous class, subclass of AA, i wish to call super(«Hello!!») within the constructor of the anonymous class, without to pass a parameter to the subclass instantiated.

    The code above prints «default». So how to print «Hello!!», Invoking the constructor with arguments ?
    The code below prints «Hello!!»

    Now, how to do prints «Hello!!» invoking super(«Hello!!»)? I would need a constructor in anonymous class.

    Thus arose the question raised, and the final conclusion was that this is not possible.

    Источник

    Anonymous Inner Class in Java

    Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

    Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.

    The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

    // Test can be interface,abstract/concrete class Test t = new Test() < // data members and methods public void test_method() < . . >>;

    Now let us do discuss the difference between regular class(normal classes) and Anonymous Inner class

    • A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time.
    • A regular class can extend a class and implement any number of interfaces simultaneously. But anonymous Inner class can extend a class or can implement an interface but not both at a time.
    • For regular/normal class, we can write any number of constructors but we can’t write any constructor for anonymous Inner class because the anonymous class does not have any name and while defining constructor class name and constructor name must be same.

    Accessing Local Variables of the Enclosing Scope, and Declaring and Accessing Members of the Anonymous Class

    Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:

    • An anonymous class has access to the members of its enclosing class.
    • An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
    • Like a nested class, a declaration of a type (such as a variable) in anonymous class shadows any other declarations in the enclosing scope that have the same name.

    Anonymous classes also have the same restrictions as local classes with respect to their members:

    • We cannot declare static initializers or member interfaces in an anonymous class.
    • An anonymous class can have static members provided that they are constant variables.
    • Fields
    • Extra methods (even if they do not implement any methods of the supertype)
    • Instance initializers
    • Local classes

    Anonymous inner classes are generic created via below listed two ways as follows:

    Now let us take an example with which we will understand anonymous inner class, let us take a simple program

    Источник

    Java Anonymous Class

    In java, we can create a class inside another class called a nested class, and we can create a class with no name,i.e called an Anonymous Class.

    In simple terms, a nested class with no name and only one object is created, said as Anonymous Class.

    Note: It must be defined in another class.

    Looking at the diagram, we see that anonymous classes along with local and nonstatic member ones form the so-called inner classes. Together with static member classes, they form the nested classes.

    In the anonymous class we can declare the following:

    // AnonymousClass = interface,abstract/concrete class. AnonymousClass c = new AnonymousClass() class TUFOuterClass < // defining anonymous class object1 = new Type(parameterList) < // body of the anonymous class >; >

    Note: Anonymous classes are defined inside an expression. So, the semicolon is used at the end of anonymous classes to indicate the end of the expression.

    Example of Anonymous Class:

    Java Code

    abstract class CheckClass < abstract void check(); >class TUF < public static void main(String args[]) < // defining anonymous class CheckClass p = new CheckClass() < // body of anonymous class void check() < System.out.println("Hello there Anonymous here!"); >>; p.check(); > >

    Output: Hello there Anonymous here!

    Ways to create Anonymous Class:

    By Extending Class:

    Java Code

    //Program to illustrate Anonymous Inner class by extending other class class Tuf < public void display() < System.out.println("Inside the TUF class"); >> class AnonymousDemo < public void createClass() < // creation of anonymous class extending class Tuf Tuf p1 = new Tuf() < public void display() < System.out.println("Inside an anonymous class."); >>; p1.display(); > > class TestAnnonymousInner < public static void main(String[] args) < AnonymousDemo an = new AnonymousDemo(); an.createClass(); >>

    Output: Inside an anonymous class

    By Implementing Interface:

    Java Code

    //Program to illustrate Anonymous Inner class by implementing interface class TUF < public static void main(String[] args) < //Anonymous Inner class that implements interface Runnable r = new Runnable() < public void run() < System.out.println("Child!"); >>; Thread t = new Thread(r); t.start(); System.out.println("Parent!"); > >

    Output:
    Parent!
    Child!

    Difference Between Regular and Anonymous Inner Class

    • A general class can implement any no. Of interfaces at a time. Anonymous inner class can implement only one interface at a time.
    • With anonymous we cannot write a constructor because the anonymous inner class does not have a name and the name of the constructor should be the same as the class name.

    Important point:

    • Anonymous class has no name
    • It can be instantiated only once
    • It is usually declared inside a method or a code block, with curly braces ending with a semicolon.
    • It is accessible only at the point where it is defined.
    • It does not have a constructor simply because it does not have a name
    • It cannot be static

    Special thanks to Amisha Kumari for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article

    Источник

    Читайте также:  Css distance between elements
    Оцените статью