New to Java Need help with constructors


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: New to Java Need help with constructors

  1. #1
    Join Date
    Feb 2006
    Posts
    4

    New to Java Need help with constructors

    Someone let me know where im going wrong. I need basic examples please .

    Heres my code.

    public class Grades
    {
    String ssn;
    String name;
    String grade;

    Grades(String s, String n, String g)
    {
    ssn = s;
    name = n;
    grade = g;
    }
    }

    class Student{
    public static void main(String args[])
    {
    Grades John = new Grades(351-63-2182, John, A);
    Grades Kris = new Grades(432-35-2562, Kris, B);

    System.out.println(John + " " + Kris);
    }
    }

    When i attempt to compile this code i get an error. Any help possible would be much appreciated.

    Thanks!

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Your constructor accepts three Strings. You're passing in 351-63-2182, John, A. You probably mean to pass in "351-63-2182", "John", "A".

    Also, note that when printing objects (when you do System.out.println(John + " " + Chris);) it will convert them to Strings using their toString() method. Since you have not overrid the toString() method, it will use the toString() method from the object class, which prints memory.

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 04:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  4. Replies: 0
    Last Post: 02-01-2002, 12:08 AM
  5. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center