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 19 of 19

Thread: Java has a default constructer, so why there is a mistake at

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java has a default constructer, so why there is a mistake at

    Hi

    It is in Java.

    class fd {
    int x;
    void generator (int i){ fd o=new fd (i);}
    }

    class ibj2 {
    public static void main (String args[]){
    fd ob=new fd ();
    for (int j=0; j <=10; j++) ob.generator (j);
    }
    }

    It is supposed that x=0 automatically since Java has a default constructer.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    First of all, it would be useful to use proper code tags to preserve syntax highlighting.
    Second, if you could show us the exact error you get it will become much easier for us to give you a proper answer. Right now, we have to guess what the error is.

    I guess the error comes because your class "fd" does not declare any constructors, but you are trying to use a non-default constructor in your code.
    The line in question is:
    void generator (int i){ fd o=new fd (i);}
    Here you try to pass the argument "i" to the constructor. But the default constructor does not take any parameters.

  3. #3
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    when I compiled the program, this is what I got as error:-

    constructor fd in class fd cannot be applied to the given types;
    required: no arguments;
    found: int;

    If you still didn't understand, then let me show you:-

    fd o=new fd (i);

    Here you cannot write i in the brackets () as "(i)". You can write

    fd o=new fd ();

    but that "might" defeat the purpose of your making the program. I'm not sure if it actually will.

    Thank you for asking.

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    The only reason to pass a variable to a constructor is done for assignment purposes.

    public class Sample
    {
       private int myInt;
     
       // explicitly defined constructor
       public Sample(int defaultArg)
       {
          myInt = defaultArg;
       }
    }
     
    // ========== //
    class Execution
    {
        public static void main(String[] args)
       {
          // this is passed to to constructor when object is created
          Sample samp = new Sample(5);
       }
    }

    As Cornix said, default constructors do not take arguments - just as implicitly declared constructors
    (programmer defined) cannot have any return type.

    Abhliash was also correct in what he said as well. Default constructors are generated by
    the compiler if the programmer does not explicitly declare one within the executing code.
    Generally - a default constructor will just create a instance of the object for a class and
    assign any instance data members values - usually null.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Thanks a lot to all of you

    --- Update ---

    I just thought that the j variable of for, would has been put directly in the generator method, it seems that it must be put into the instance variable x first. Am I wrong?

    --- Update ---

    I am just trying to understand the mechanism of it.

  6. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    I am just trying to understand the mechanism of it.
    When you declared the iterator variable 'j' in the main method - it was declared in the scope
    of the body of the for statement. With each iteration of the loop - you passed the value of "j"
    to the called method in the other defined class.

    when you passed that variable. it was passed by value to that method. Control then moved
    to the defined class and the method with the integer parameter took the control variable as
    it's argument - no problem here.

    Within the body of that method - you attempted to create a constructor but as stated it was incorrect.
    So - here is what the best solution would be:

    public class Base
    {
       private int x; // instance of 'x' this is known only to Base
     
       // explicitly declared constructor - (this is not a copy constructor)
       public Base(int i) // expecting an integer parameter when object is created
       {
          this.x = i; // ok - x now holds the value stored in i
       }
    }
     
    // ==== //
    public class Parent
    {
       public static void main(String[] args)
       {
           // let's create an object - and pass the integer argument
           // to the parameter of the constructor in the Base class 
          Base bse - new Base(10);
       }
    }

    Additional advice: Anything created outside of the body of the Base class
    is only local (visible) to that scope. Base class does not know it would exist.
    Java passes everything by value - it does not incorporate references. So, if you
    attempted to pass a value to a method and modify it, and return it - it would not be changed.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Thanks.

    I have read that the contents of a String object are immutable, but I could change them as I wish, check this out:

    class a {
    public static void main (String args []) {
    String x= "mummy"; System.out.println (x);
    x="daddy"; System.out.print (x);

    }
    }

    As I see, there is no point of knowing that they cannot be altered since I can change the contents.
    Could anyone tell me what I have missed here?

    --- Update ---

    Is there any new updates from Oracle that has applied on String class?

    --- Update ---

    Plus, how can I retrieve a reference variable for a string object? As I do for an variable of a class.

    --- Update ---

    * a variable of a class type

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    I think you are confusing objects and variables.
    In your example you have one variable, it is called x.
    But you have 2 different String objects. One of these Strings is "mummy" and the other String is "daddy".
    You assign these objects to your variable, but the Strings themselfs never change. Only the reference of the variable x changes.

    After
    x="daddy";
    the String "mummy" still exists, and it still reads mummy.

    If you want to make sure that the reference of your variable x will never change you can declare it final. For example:
    final String x = "mummy";
    Now you could not assign a different value to this variable anymore.

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    How could I get the String "mummy" again after changing the reference variable?

    Check out this code:
    class cat1 {
    int x;
    }
     
    class tot {
     public static void main ( String args [ ] ) {
      String y=new String ();
      cat1 z= new cat1 ();
      System.out.println (y + "\n" + z);
     
     
    }
    }
    Since y is a reference variable for a String object, why cannot I print it like the variable of z?
    Last edited by magDreameR; June 15th, 2014 at 01:29 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    why cannot I print it
    Please copy and paste here the full text of the error message.
    Or explain what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    'y' is just an instance of a String. 'y' also holds nothing at the time
    it is printed (except possibly null) - what did you expect it to print?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  12. #12
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Works for me:
    public class Test {
     
    	public static void main(String[] args) {
    		String s = new String();
    		Integer i = new Integer(42);
     
    		System.out.println(s+"\n"+"i");
    	}
    }

  13. #13
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    y is supposed to be an address that refers to a String object, why cannot I print it out like I did to z?
    Is there a difference between objects of built - in classes and the other objects?
    Last edited by magDreameR; June 15th, 2014 at 01:26 PM.

  14. #14
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    As I said, you can print it.
    The program I posted above works like a charm.

    If you are getting an error, please provide it to us.

  15. #15
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Cornix: I have two classes in my code.

    --- Update ---

    My code has no error. Please, understand my point. y and z are both references to objects but I can only print one of them.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Hint for printing Strings: Add delimiters to show contents:
      System.out.println("y="+y + "<\n"+ z);

    Try that and see what is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    So, y is not a reference variable? It doesn't have an address for an object? Seems like the normal variables. If you excute my code you will see my point.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Did you look very carefully at what your code printed? Can you see that an empty, blank line was printed first?

    Try my code to see what is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java has a default constructer, so why there is a mistake at

    Quote Originally Posted by magDreameR View Post
    So, y is not a reference variable? It doesn't have an address for an object? Seems like the normal variables. If you excute my code you will see my point.
    y is a reference to a String object in your code. And I am sure that you can print it.
    But the y in your example code is an empty String. So perhaps you think that you cant print it because you dont see anything.

Similar Threads

  1. Default Write a Java program to simulate an online shopping cart.
    By aldos7 in forum What's Wrong With My Code?
    Replies: 36
    Last Post: December 16th, 2013, 06:45 AM
  2. Replies: 2
    Last Post: November 24th, 2012, 09:18 AM
  3. Assign Default values to the JTextfield in java swing
    By sjohnfernandas in forum AWT / Java Swing
    Replies: 1
    Last Post: December 17th, 2011, 06:59 AM
  4. Java server Faces and Oracle
    By rcbandit in forum JDBC & Databases
    Replies: 0
    Last Post: December 8th, 2011, 02:59 PM
  5. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 04:12 PM