Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-15-2007, 06:01 AM
Member
 
Join Date: Nov 2007
Posts: 12
bluegreen7hi is on a distinguished road
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!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-15-2007, 06:10 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,145
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-15-2007, 06:44 AM
Member
 
Join Date: Nov 2007
Posts: 12
bluegreen7hi is on a distinguished road
Thank you very much!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor calling ravian New To Java 2 12-22-2007 07:53 PM
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 10:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 10:01 AM
Help with constructor in java mathias New To Java 1 08-07-2007 02:00 AM
Constructor Chaos derrickD Advanced Java 1 04-27-2007 09:02 PM


All times are GMT +3. The time now is 09:37 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org