• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question regarding default constructors  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have this simple class with a constructor:
============================================================

public class ScopeTest {

public static void main(String[] args) { ScopeTest tst = new ScopeTest(); }

public ScopeTest(String a) { System.out.println("ScopeTest(String a) : " + a); }

}

============================================================
This class generates these errors when compiling:

ScopeTest.java:3: cannot find symbol
symbol : constructor ScopeTest()
location: class ScopeTest
public static void main(String[] args) {ScopeTest tst = new ScopeTest();}
^
All the books and tutorials I read say that the compiler invokes the constructor of the super class (Object in this case)
if it can't find one with the right signature in the class.

BUT..... if I comment out this class's constructor it compiles and works and seems to use
the super class constructor (Object).

Thanks in advance,

Mario
Can anyone explain???
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've defined a constructor that accepts an argument, but then call one that doesn't -- hence the error.

When you define no constructors, a default constructor is created that accepts no arguments. This does not happen if you explicitly define one yourself.
 
Mario Michetti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear, that's what I had understood but was not sure. It kinda makes sense. I give you control over how objects get instaTIATED.

Cheers,

Mario

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!