Results 1 to 3 of 3
  1. #1
    javamann is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default help with java code - new to java

    Hi All

    I am creating a class called Book which contains getters and setters for book attributes.
    Java Code:
    class Book {
    private String title;
    private String ISBN;
    I have a constructor used as the setter as follows:
    public Book (String myTitle, String myISBN, String ....){  
    this.title = myTitle;
    this.ISBN =  myISBN;
    within the class i created getters as follows:
    public String getTitle(){
                  return title;
           }//end method getTitle
    public String getISBN(){
                  return ISBN;
    now when I create new objects in my other class, I do

    Book Book1 = new Book {"title123","8888",etc}

    All this is working well so far.

    now I know i can get each attribute of the book by Book1.getTitle() but how do I get ALL the attributes of each book in one so that i can refer to Book1 as an object rather than referring to each attribute of Book1.

    I need this to store all my books in an ArrayList, print them, sort them in different orders etc.

    Last edited by sunde887; 11-04-2011 at 11:48 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    kkji is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Re: help with java code - new to java

    Please post full code and your current output.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power

    Default Re: help with java code - new to java

    One nitpick and then some help: When naming variables use camel case (first letter first word lowercase, first letter of 2nd+ words capitalized), so book1, not Book1. Classes have the first letter of each word capitalized.

    If you want to do things with the variable, you would use the getters, however; if you simply want to neatly print a list of books, look into overriding the toString method.

    Java Code:
    @Override public String toString(){
      //do stuff here
      return someString;

Similar Threads

  1. Replies: 1
    Last Post: 07-14-2011, 01:14 AM
  2. Replies: 13
    Last Post: 06-29-2011, 07:25 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 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