1

Ok, so I'm in need of some basic help. Here's the tutorial I'm trying to learn from (http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html), but I'm confused as to how to actually pass data into it. The problem is I've got my Pascal brain on when trying to learn java...

Here's my code. What am I doing wrong?

public class OrigClass{
    public static void main(String[] args){
        StudentData(17, "Jack"); //error here: the method StudentData(int, String) is undefined for the type OrigClass
    }

    public class Student{
        public void StudentData(int age, String name){
            int sAge = age;
            String sName = name;
            System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
        }
    }
}

Thanks in advance for the help :)

5 Answers 5

5

Constructor is not just a method: you need to give it the same name as the class, and call it with a new operator, like this:

public class Student{
    // Declare the fields that you plan to assign in the constructor
    private int sAge;
    private String sName;
    // No return type, same name as the class
    public Student(int age, String name) {
        // Assignments should not re-declare the fields
        sAge = age;
        sName = name;
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}
// This goes in the main()
Student sd = new Student(17, "Jack");
Sign up to request clarification or add additional context in comments.

2 Comments

He has StudentData as method not Student might be his understanding to call the method is wrong.
@amicngh Right - that's one of the things that's wrong with his alleged "constructor" :-)
2

There are several issues in your code.

Firstly, you've defined the constructor of StudentData like a normal method - constructors have no return type.

Secondly, you need use the new keyword to create a non-primitive object in Java.

public class OrigClass{
    public static void main(String[] args){
        new Student(17, "Jack"); 
    }
}

public class Student{
   public Student(int age, String name){
       int sAge = age;
       String sName = name;
       System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
   }
}

9 Comments

OP also forgot to declare the fields, assigning constructor args to local variables inside the constructor.
That in and of itself won't prevent his code from compiling and executing, it just means that his results will be nonsensical :)
Note that Student is an inner class of OrigClass so you would have to do (new OrigClass()).new Student(17, "Jack") in order to instantiate a Student.
@dasblinkenlight. My understanding is StudentData is method rather than assuming this Student construtor.
@mcfinnigan np, it wasn't obvious to me until I reformatted the code. Although I would suggest your solution over the nested class approach.
|
2

If I am assuming you have written Student class correctly without considering Java naming convention in mind and StudentData is method then the way to call method StudentData is incorrect.First create object ofStudent class and then call the method

Update: Considering Student is inner class

 public static void main(String[] args){
    new OrigClass().new Student().StudentData(17, "Jack");// Considering Student is inner class
  }

2 Comments

Speaking of convention, Java is a word and not an acronym, and thus should not be written in all uppercase.
@SteveKuo seconded :).
1
public class OrigClass {

    public static void main(String[] args) {
        Student obj = new Student();
        obj.studentData(17, "Jack");

    }
}

public class Student {   

    public void studentData(int sName, String sAge) {
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}

Comments

0

so, first of all, you can't access a non-static member(StudentData(int age, String name)) from within a static method(public static void main(String[] args)). So if you want to access the method from within your static main method you would need to do the following:

  1. Create an alternate method(for eg: name it xyz()), that will create an object(eg: std) for your inner class Student and then calls the StudentData method with the syntax std.StudentData(17,"JACK");.
  2. Next thing call this alternate method from the static main method by creating an object (eg: obj) of the outer class and invoking the alternate method with the help of this obj like obj.alternateMethodName();

Here's the complete code showing how you can achieve the desired output:

public class OrigClass{
public static void main(String[] args){
    OrigClass obj= new OrigClass();
    obj.getStudentData();
}
public void getStudentData(){
    Student std = new Student();
    std.StudentData(17, "Jack");
}

public class Student{
    public void StudentData(int age, String name){
        int sAge = age;
        String sName = name;
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.