Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Static Methods and Properties A) Explain what the static keyword does when used

ID: 3903508 • Letter: S

Question

Static Methods and Properties

A) Explain what the static keyword does when used with a method in a class and give an example of a static method from Java’s own libraries:

B) Given this class snippet: public class Student {

private String firstName; // first name

private String lastName; // last name

private String email; // email address

private int catalogYear; //catalog year for student entrollment

// construct a new student with given fields

public Student(String first, String last, String email, int section) {

this.first = first;

this.last = last;

this.email = email;

this.section = section; }

The school would like to add a unique identifier to every student as each record object is created. Modify the class properties and constructor to achieve this. Show the result below:

Explanation / Answer

Question A:

A static keyword is used to make a program memory efficient. It is used to refer to a common property to all objects of a class.

For example if we want to create a set of students objects of college, then we can declare the college name as static because it is common for all the Students and also static variable is also created once in the memory.

class Student{  
int rollno;
static String college="BITS";  
}  

Program B:

Here i am want to create a new property for the student class called rollno, which is assigned by a static variable. This can be a unique identifier to every student.

public class Student {
private int rollno; // roll no
static int count = 1; // static variable
private String firstName; // first name

private String lastName; // last name

private String email; // email address

private int catalogYear; //catalog year for student entrollment

// construct a new student with given fields

public Student(String first, String last, String email, int section) {

this.rollno = count;

count++; // Incrementing to be unique for the next student

this.first = first;

this.last = last;

this.email = email;

this.section = section; }

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote