Results 1 to 2 of 2
  1. #1
    mathias is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with constructor in java

    Hi, I've a question about this code:
    Java Code:
    public class Test {
    
      public Test(){}
      
      public static void main(String args[]) 
      {
        System.out.println(new Test().i);
      }
    
      int i;
    }
    The Java book I'm consulting says that when you provide a parameter less constructor, you replace the Java-provided default constructor. It also states that the Java-provided default constructor initialises all attributes to a default value for example zero for an integer.

    However, if the above code is really replacing the default constructor, then how is int i being set to zero?

    I suppose in a way this seems a weird question, it's just I'm trying to get a good understanding of the basics.

    Thanks.

  2. #2
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    When you declare it it's set to zero. It doesn't really have anything to do with the constructor.

Similar Threads

  1. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  2. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  3. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 AM
  4. Use ArrayList Constructor...
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:37 AM
  5. Java constructor method
    By carl in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 02:15 PM

Posting Permissions

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