Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Construct a class that implement ActionListener with no constructor

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Construct a class that implement ActionListener with no constructor

    Hi, I am reading a passage in Java book that goes like this:

    class TimePrinter implements ActionListener {
    	public void actionPerformed(ActionEvent event) {
    		Date now = new Date();
    		System.out.println("At the tone, the time is " + now);
    		Toolkit.getDefaultToolkit().beep();
    	}
    }

    Now to use TimePrinter we would do:

    ActionListener listener = new TimePrinter();
    Timer timer  = new Timer(20000, listener);

    I am confuse about creating a
    new TimePrinter();
    when the TimePrinter class have no constructor. From what I read, if a class have no constructor, it will then call the superclass of TimePrinter, which is Object, and use the default no-arg constructor. But I think its a bit confusing for me to create a class without constructor but only a method. Can somebody lighten me up on this a little bit more?
    Last edited by striko_514; July 26th, 2010 at 08:17 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Construct a class that implement ActionListener with no constructor

    In Java, if you have no explicitly defined constructor, there's a default constructor which initializes all values to their "default" value, i.e. nulls, 0's, etc. This is what's known as a an implicit constructor. Note that once you provide even 1 constructor for the class, you no longer get the implicit constructor.

Similar Threads

  1. how to initialise set in a constructor
    By davie in forum Collections and Generics
    Replies: 3
    Last Post: March 12th, 2010, 05:35 PM
  2. calling a constructor
    By turnwellm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 08:46 PM
  3. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 01:07 PM
  4. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  5. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM