This document defines a Student class with properties for id, name, and age, and getters and setters for each. It then creates two Student objects named mark and tom, sets their property values, and prints out statements displaying each student's name and age.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
55 views1 page
Classes and Objects Example
This document defines a Student class with properties for id, name, and age, and getters and setters for each. It then creates two Student objects named mark and tom, sets their property values, and prints out statements displaying each student's name and age.
* *************************************************************************/ package lesson1; public class Student { int id; String name; int age; public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } } public class MyClass { public static void main(String[] args) { Student mark = new Student();//mark -> object or instance mark.setId(1); mark.setName("Mark"); mark.setAge(15); Student tom = new Student();//Tom -> object or instance tom.setId(1); tom.setName("Tom"); tom.setAge(14); System.out.println(mark.getName() + " is "+ mark.getAge() + " years old"); System.out.println(tom.getName() + " is "+ tom.getAge() + " years old"); } }