This should be a simple code for beginning Java;
Create a Student class with four instance variables, a default constructor, a 4-arg constructor, and getters and setters for each instance variable.
SOURCE CODE FOR STUDENT CLASS:
import java.util.*;
import java.lang.*;
import java.io.*;
class Student
{
String firstName; //declaring four instance variables..
String lastName; //instance variables are those which are inside a
class
int rollNo;//but outside a method or constructor..
int marks;
public Student()//default constructor..
{
System.out.println("Default constructor called..");
}
public Student(String fName, String lName, int rNo, int m)
//parameterized constructor with 4 variables..
{
firstName=fName;
lastName=lName;
rollNo=rNo;
marks=m;
}
public String getFirstName(){ //just returning the value of
instance variable which you want..
return firstName;
}
public String getLastName(){ //just returning the value of
instance variable which you want..
return lastName;
}
public int getRollNo(){ //just returning the value of instance
variable which you want..
return rollNo;
}
public int getMarks(){ //just returning the value of instance
variable which you want..
return marks;
}
public void setFirstName(String fName){
firstName=fName; //setting firstName i.e. instance variable to the
name passed in set method..
}
public void setLastName(String lName){
lastName=lName; //setting lastName i.e. instance variable to the
name passed in set method..
}
public void setRollNo(int rNo){
rollNo=rNo; //setting rollNo i.e. instance variable to the integer
passed in set method..
}
public void setMarks(int m){
marks=m; //setting marks i.e. instance variable to the integer
passed in set method..
}
}
LETS SEE AN EXAMPLE BY MAKING A MAIN CLASS WHICH EXECUTE ABOVE METHODS i.e. GETTER AND SETTER:
SOURCE CODE FOR EXAMPLE CLASS:
class Example
{
public static void main(String[] args){
Student tushar = new Student ("Tushar", "Nagpal", 80, 100);
//creating an object using parameterized constructor..
System.out.println("Values of instance variables before using
setters: ");
System.out.println(tushar.getFirstName() + " " +
tushar.getLastName()+" " + tushar.getRollNo()+" "
+tushar.getMarks());
tushar.setFirstName("MODIFIED"); //changing values using
setters..
tushar.setLastName("CHANGED");
tushar.setRollNo(1);
tushar.setMarks(0);
System.out.println("Values of instance variables after using
setters: ");
System.out.println(tushar.getFirstName() + " " +
tushar.getLastName()+" " + tushar.getRollNo()+"
"+tushar.getMarks());
//printing values using getters..
}
}
OUTPUT OF EXAMPLE:
NOTE: In the question it was written to create a Student class.. but I have taken an "Example" class also which is main class. So that you are able to see whats going on really..and it is optional..
So, Please copy entire code of both the classes separately and save them in a common directory with the name:
"Student.java" and "Example.java"
then, compile both and run "Example" class..
I have mentioned the comments wherever neccessary in the code..
HOPE IT HELPS..!!!!
For any query or problem please feel free to comment.
Thanks.
Get Answers For Free
Most questions answered within 1 hours.