Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: what's wrong with this code ?? HELP

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy what's wrong with this code ?? HELP

    public class Books {
    String Bookname;
    int Bookid;
    double Salary;

    class Books (String Bookname, int Bookid, double Salary){

    Bookname = Bookname;
    Bookid = Bookid;
    Salary = Salary;


    }
    public void print(){
    System.out.println(" the name of the book is" + Bookname);
    System.out.println( " the book's id is "+ Bookid);
    System.out.println(" and it costs" + Salary);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what's wrong with this code ?? HELP

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link. Ask specific questions, post error messages that you want help with. Give your threads meaningful titles.

  3. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: what's wrong with this code ?? HELP

    Quote Originally Posted by Roosh View Post
    class Books (String Bookname, int Bookid, double Salary){

    Bookname = Bookname;
    Bookid = Bookid;
    Salary = Salary;
    }
    When you have achieved what Greg suggested - you should look more closely at the above.
    Hint: That is not how you declare a constructor - it is not a class but part of it.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  4. #4
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: what's wrong with this code ?? HELP

    Exactly! it should be "void Books" not "class Books". Look again, a class inside a class.

  5. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: what's wrong with this code ?? HELP

    Quote Originally Posted by Abhilash View Post
    Exactly! it should be "void Books" not "class Books". Look again, a class inside a class.
    If that is a constructor (which I am almost certain it is) then it can have no return type (not even void).

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. #6
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: what's wrong with this code ?? HELP

    Yeah, maybe. But if and only if it is a constructor. Maybe its not.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: what's wrong with this code ?? HELP

    Very confusing to have a method with the same name as the class and constructor.
    method names should be verbs, class names nouns.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Lightbulb Re: what's wrong with this code ?? HELP

    Absolutely. That's actually the convention adopted by oracle and google. SeeCode Conventions for the Java Programming Language: Contents and Google Java Style.

Similar Threads

  1. what is wrong with my code???
    By koolestkid20 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 25th, 2012, 01:12 PM
  2. what's wrong with my code?
    By vanitha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 1st, 2012, 07:23 AM
  3. What is wrong with this code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 24th, 2012, 12:31 AM
  4. What is wrong with my code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 23rd, 2012, 02:40 PM
  5. want wrong with my code
    By drum in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:06 AM