Tuesday 1 December 2015

Java Tutorial : Java method returning class


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=Rz9LBGX3ADM&list=UUhwKlOVR041tngjerWxVccw

Student.java
public class Student
{

    private String name;
    private int age;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

}
StudentTest.java
public class StudentTest
{

    public static void main(String[] args) throws InterruptedException
    {
        Student johnReferenceVariable = getStudent("John", 25);

        System.out.println("Name : " + johnReferenceVariable.getName());
        System.out.println("Age  : " + johnReferenceVariable.getAge());

        System.out.println("--------------------------------");

        Student peterReferenceVariable = getStudent("Peter", 55);

        System.out.println("Name : " + peterReferenceVariable.getName());
        System.out.println("Age  : " + peterReferenceVariable.getAge());

    }

    /*
     * Returning a Class:
     * 
     * When a method uses a class name as its return type, the class of the type
     * of the returned object must be either a subclass of, or the exact class
     * of, the return type.
     */
    public static Student getStudent(String name, int age)
    {
        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        return student;
    }

}
Output
Name : John
Age  : 25
--------------------------------
Name : Peter
Age  : 55
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/MethodDemoReturnClassApp.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/MethodDemoReturnClassApp

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/68054fa19b88e552162495a83ba964283753af15/BasicJava/MethodDemoReturnClassApp/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment