Results 1 to 3 of 3
Thread: Constructor Help
- 11-15-2007, 05:01 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
Constructor Help
Hi there, I'm new to Java. I'm taking an AP Java course in school and one of my assignments is to make a program that greets me by my name. However, I must declare the constructor explicitly. Here are the requirements:
1. Class name: MyGreetings2
2. Have two public string fields: lastName and firstName.
3. Make the constructor set lastName to your last name and firstName to your first name.
Here is the code that I've written:
public class MyGreetings2 {
public MyGreetings2() {
String firstName = Nick;
String lastName = Johnson;
}
public static void main(String[] args);
System.out.println("Hello, " + firstName + " " + lastName)
}
}
When I compile, I get two errors:
C:\JavaPrograms\MyGreetings2.java:10: <identifier> expected
System.out.println("Hello, " + firstName + " " + lastName)
^
C:\JavaPrograms\MyGreetings2.java:10: illegal start of type
System.out.println("Hello, " + firstName + " " + lastName)
Is there something huge I'm missing or doing wrong here? I've been trying to figure this out for hours. Oh, and I'm using JCreator for an IDE. Any help would be greatly appreciated.
Thanks!!
- 11-15-2007, 05:10 AM #2
Java Code:public class MG2 { String firstName; String lastName; public MG2() { firstName = "Nick"; lastName = "Johnson"; } public static void main(String[] args) { // <--- errant semi-colon MG2 app = new MG2(); System.out.println("Hello, " + app.firstName + " " + app.lastName); } }
- 11-15-2007, 05:44 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Constructor calling
By ravian in forum New To JavaReplies: 2Last Post: 12-22-2007, 06:53 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Help with constructor in java
By mathias in forum New To JavaReplies: 1Last Post: 08-07-2007, 01:00 AM -
Constructor Chaos
By derrickD in forum Advanced JavaReplies: 1Last Post: 04-27-2007, 08:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks