Results 1 to 4 of 4
  1. #1
    ben2470 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Unhappy Declaring class in main class problem

    So i declared a class in main class but it seems there's error when i compile:

    constructor xx in class xx cannot applied to given types

    this is my java class:

    public class trainer extends person{
    String classType;

    public trainer(String name, String gender, String address, int id, String classType) {
    super(name,gender,address,id);
    this.classType=classType;
    }

    public String toString(){
    return super.toString()+"\nClass incharge :"+classType;
    }
    }



    And this is the way i declared in main class:

    trainer tr = new trainer();

    And what i want to do is:

    tr.toString();

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Declaring class in main class problem

    I don't see a no-arg constructor anywhere in the code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ben2470 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Re: Declaring class in main class problem

    Quote Originally Posted by JosAH View Post
    I don't see a no-arg constructor anywhere in the code ...

    kind regards,

    Jos
    I'm new to Java and what it means no-arg constructor?
    any examples?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,280
    Rep Power
    6

    Default Re: Declaring class in main class problem

    Let me put it differently then.

    Java Code:
    public trainer(String name, String gender, String address, int id, String classType) {
    5 parameters.

    Java Code:
    trainer tr = new trainer();
    0 parameters. You demand that information be passed when creating the trainer yet then you absolutely refuse to do it.

    This does work:

    Java Code:
    trainer tr = new trainer("Bill Gates", "Female", "Redmond 90210", 5, "glasses");

    PS: the standard naming conventions is that each word in a classname starts with a capital letter; you should follow that convention. So don't call your class 'trainer', call it 'Trainer'. And then you can write code like this:

    Java Code:
    Trainer trainer = new Trainer("Bill Gates", "Female", "Redmond 90210", 5, "glasses");
    and not get confused what is a classname and what is a variable. On top of that you are not forced to use confusing variable names such as 'tr'.
    Last edited by gimbal2; 08-01-2014 at 05:47 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2014, 06:09 PM
  2. Replies: 16
    Last Post: 11-20-2013, 01:43 PM
  3. Replies: 1
    Last Post: 11-29-2012, 10:46 AM
  4. Problem with main class
    By otacon in forum New To Java
    Replies: 6
    Last Post: 12-14-2010, 04:46 PM
  5. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM

Tags for this Thread

Posting Permissions

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