Results 1 to 6 of 6
  1. #1
    prabhajan is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Question why should we invoke empty construtor

    i have seen one program, in that the empty class constructor called on main method, why should we call that? even it has several public methods?
    In java, the construtor invoked automatically, but here why should we call within that or why should we create object for the class without the variable? kindly clear my doubt

    here is the code format.

    public class Sample
    {
    public Sample()
    {
    }
    public void add()
    {
    ..................
    ..................
    }
    public void sub()
    {
    ....................
    ...................
    }
    private Integer getValue()
    {
    return .........
    }

    public static void main(String a[])
    {
    new Sample();
    }
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    It doesn't make much sense to make an object without a reference to it. As to why make an object in the first place, try calling one of the methods inside main and you'll see why (static vs non-static).
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    It is one way of getting out of main() quickly, though.

    The main() method is just the entry point for Java, so the quicker you get out of it the better (IMO). I have used this when I've done standalone apps:
    Java Code:
    public class MyApp {
        public static void main(String a[])
        {
            new MyApp().run();
        }
    
        private MyApp() {
            // Some basic setup code if needed.
        }
        private void run() {
            // My actual app.  And it's an object so I don't have to worry about static/non-static nonsense.
        }
    etc etc

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Haven't thought of this example, even though I used it before. Nice catch!
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    prabhajan is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Question constructor and main method

    thank you both, this is another one regarding constructor and main method


    class Sample()
    {
    Sample()
    {
    System.out.println("Hello world");
    }
    public static void main(String a[])
    {
    new Sample();
    }
    }

    Constructor should be invoked at the time of object creation, right? the object creation should be done within main() ? or can we create in outside of the main method?

  6. #6
    prabhajan is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    i want to learn java in a clearcut way, kindly give a guidance for that.

Similar Threads

  1. reflection invoke method
    By pprl in forum New To Java
    Replies: 12
    Last Post: 11-16-2010, 05:43 PM
  2. How to invoke thread2 after the thread1
    By erosszz_jp@yahoo.co.jp in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-02-2010, 04:31 PM
  3. How invoke open dialog?
    By artemff in forum CLDC and MIDP
    Replies: 4
    Last Post: 12-27-2009, 03:18 PM
  4. Invoke Applet
    By chankokchern in forum Java Applets
    Replies: 1
    Last Post: 07-29-2008, 04:35 PM
  5. Re address to jsp to that I invoke it
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •