Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By BIOS

Thread: Help with class

  1. #1
    BIOS is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Help with class

    I can't understand why this won't work. Basically i'm looking to put a print message in the constructor of the class but im getting an error:

    Java Code:
    illegal start of type
    System.out.println("Hello!");
                       ^
    Java Code:
    public class Student{
    
    // Attributes
    
    String name;
    String studentID; 
    String birthDate;
    String address;
    String major;
    double gpa;
    Professor advisor;
    
    System.out.println("Hello!");
    
    }
    Any ideas?

    Cheers

    BIOS

  2. #2
    SFNYC is offline Member
    Join Date
    Sep 2010
    Posts
    16
    Rep Power
    0

    Default Re: Help with class

    You need 'main'

    (e.g.)

    Java Code:
     public static void main(String[] args)
    {
    System.out.println("Hello!");
    }

  3. #3
    BIOS is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Help with class

    Hey SFNYC. Thanks for the reply. How come I can't just put it into the constructor? I'm just looking to get some sort of visual feedback from the console when I run my main class so that when I instantiate a class It prints something to the console. Ex:

    Java Code:
    // Main Class
    
    public class MainClass {
    
    public static void main(String[] args){
    
    Student student = new Student();
    Professor professor = new Professor();
    
        }
    }
    Are you saying I need a main method in both my Professor and Student classes to get them to have a print message in their respective constructors?

    Cheers

    BIOS

  4. #4
    BIOS is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Help with class

    Ah I see my problem. Apologies. I need to include it in the constructor which is separate from the class declaration:



    Java Code:
    // Student class
    
    public class Student{
    
    // Attributes
    
    String name;
    String studentID; // camel casting
    String birthDate;
    String address;
    Department major;
    double gpa;
    Professor advisor;
    
    
    // constructor
        Student(){
        System.out.println("Hello!");
        }
    
    
    }
    Fubarable likes this.

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  2. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  3. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

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