Results 1 to 3 of 3
  1. #1
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default 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!!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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);
        }
    }

  3. #3
    bluegreen7hi is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 06:53 PM
  2. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  4. Help with constructor in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 01:00 AM
  5. Constructor Chaos
    By derrickD in forum Advanced Java
    Replies: 1
    Last Post: 04-27-2007, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •