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 4 of 4

Thread: Java simple program error?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java simple program error?

    Hiya! I just recently learn about the "this" keyword and constructor.I'm a Java newbie.I'm having a few errors within my code and am quite baffled.
    I would greatly appreciate if you could explain what I done wrong.

    Thanks everyone!
    class Student{

    // instance variables
    String Name;
    String Surname;

    public Student(String N,String S)

    {
    this.Name=N;
    this.Surname=S;
    }

    public String getandprint()

    {

    System.out.println("The student name is:"+"N"+"S");

    } //missing return statement



    }

    class System1{

    public static void main(String [] args)

    {

    Student obj = new Student(Jim,Jones); // cannot find symbol name for names

    //System.out.println("The student name is:"+obj.Name+obj.Surname);


    }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Java simple program error?

    in statement Student obj = new Student(Jim,Jones); java assumed that Jim and Jones are variables but you never declared them as a String variables.
    So if you want to pass Jim and Jones as a String object you must enclosed them with double qoutes.
    so it should be like this Student obj = new Student("Jim","Jones");

    or you can declare a variables whose values are "Jim" and "Jones" then pass the variables to the constructor

  3. #3
    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: Java simple program error?

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  4. #4
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: Java simple program error?

    Adding some extra info to dicdic, if you want to use this. you would have to declare a String name, then in your constructor, your parameter in your constructor Student() you need the name of your string to be the same: Student(String name)

    then you can use this.name = name to refer to name in your constructor, if they aren't the same, you don't need to use this.

Similar Threads

  1. [SOLVED] Java Simple Rectangle Program Error
    By XP360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2013, 11:07 PM
  2. Simple I/O Java Error
    By Prox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2011, 04:48 PM
  3. Simple Java Program
    By Robbiep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 24th, 2011, 07:27 AM
  4. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM