Board index » jbuilder » Overloaded constructor question

Overloaded constructor question


2003-08-07 10:40:41 PM
jbuilder22
Hi,
I'm new to Java and trying to create some programs. With this source:
Quote
import java.util.*;

class Test {
Test() {
System.out.println("constructor");
}
Test(String s) {
System.out.println("constructor" + s);
}
}

public class DefaultConstructor {
static Test monitor = new Test(s);
public static void main(String[] args) {
new Test();
monitor.expect(new String[]);
}
}
I get these compiler-errors:
Quote
/export/home/Huub/DefaultConstructor.java:13: Undefined variable: s
static Test monitor = new Test(s);
^
/export/home/Huub/DefaultConstructor.java:16: Method expect(java.lang.String[])
not found in class Test.
monitor.expect(new String[]);
^
/export/home/Huub/DefaultConstructor.java:16: Array dimension missing.
monitor.expect(new String[]);
^
3 errors

Can somebody please help me out? The exercise was: add an overloaded
constructor that takes a String argument and prints it along with your
message.
Help very much appreciated..
Thanks
Huub
 
 

Re:Overloaded constructor question

This may solve it
public class DefaultConstructor {
String s="My String Here";
static Test monitor = new Test(s);
public static void main(String[] args) {
new Test();
monitor.expect(new String[]);
}
}
"Huub" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

I'm new to Java and trying to create some programs. With this source:

>import java.util.*;
>
>class Test {
>Test() {
>System.out.println("constructor");
>}
>Test(String s) {
>System.out.println("constructor" + s);
>}
>}
>
>public class DefaultConstructor {
>static Test monitor = new Test(s);
>public static void main(String[] args) {
>new Test();
>monitor.expect(new String[]);
>}
>}

I get these compiler-errors:

>/export/home/Huub/DefaultConstructor.java:13: Undefined variable: s
>static Test monitor = new Test(s);
>^
>/export/home/Huub/DefaultConstructor.java:16: Method
expect(java.lang.String[])
>not found in class Test.
>monitor.expect(new String[]);
>^
>/export/home/Huub/DefaultConstructor.java:16: Array dimension missing.
>monitor.expect(new String[]);
>^
>3 errors
>

Can somebody please help me out? The exercise was: add an overloaded
constructor that takes a String argument and prints it along with your
message.

Help very much appreciated..

Thanks

Huub

 

Re:Overloaded constructor question

See comments inline with your code:
Hope this helps.
Ed
In article <MqHZa.260442$ XXXX@XXXXX.COM >,
XXXX@XXXXX.COM says...
Quote
Hi,

I'm new to Java and trying to create some programs. With this source:

>import java.util.*;
>
>class Test {
>Test() {
>System.out.println("constructor");
>}
>Test(String s) {
>System.out.println("constructor" + s);
>}
>}
>
>public class DefaultConstructor {
// You need to define the variable s.
// Add this line:
// String s = "myString";
Quote
>static Test monitor = new Test(s);
>public static void main(String[] args) {
>new Test();
// There is no expect() method defined.
>monitor.expect(new String[]);
// 'new String[]' is incorrect syntax to create a String array.
// Assuming the execute() method is defined elsewhere, you could
// pass an anonymous array as an argument to execute() like this:
// monitor.expect(new String[] {"string1", "string2"});
Quote
>}
>}

I get these compiler-errors:

>/export/home/Huub/DefaultConstructor.java:13: Undefined variable: s
>static Test monitor = new Test(s);
>^
>/export/home/Huub/DefaultConstructor.java:16: Method expect(java.lang.String[])
>not found in class Test.
>monitor.expect(new String[]);
>^
>/export/home/Huub/DefaultConstructor.java:16: Array dimension missing.
>monitor.expect(new String[]);
>^
>3 errors
>

Can somebody please help me out? The exercise was: add an overloaded
constructor that takes a String argument and prints it along with your
message.

Help very much appreciated..

Thanks

Huub


 

{smallsort}