Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
Static Variables and methods are part of heap memory. The static variables and methods can be accessed by the objects and can be accessed without even creating an object.
In order to use any instance variables or methods, you must create an object. It is impossible to use an instance variable or call an instance method without creating a method.
But, static variables or methods are not like that. You can call those either using an object and without even creating an object. It plays a very important role in different scenarios.
Let me explain it with an example:
public class Student{
public static void main(String []args){
// There are 10 student objects are being created.
Student obj[] = new Student[10] ;
for(int i = 0; i < 10; i++) {
obj[i].getMarks();
obj[i].display();
}
}
}
If you see the example, I created an array of 10 student objects. We need to display college code which is common for all the students. If we do not have a static variable, you need to allocate 10 memory space for 10 student objects. If we have 1000 objects then 1000 memory space. This problem can be avoided by using a static variable college code which can be accessed by all the objects.
public class Student{
public static void main(String []args){
// There are 10 student objects are being created.
Student obj[] = new Student[10] ;
// static
static int collegeCode = "123";
for(int i = 0; i < 10; i++) {
obj[i].getMarks();
obj[i].display();
}
System.out.println("Hello World");
}
}
Similary, the static method would be useful, if you want to display some generic information. You don't need to create an object for the same.
Please do comment for any doubts or modifications!
Explain how class (static) variables and methods differ from their instance counterparts. Give an example of...
Java Homework Question: Explain how class (static) variables and methods differ from their instance counterparts. Give an example of a class that contains at least one class variable and at least one class method. Don't forget to provide the code. Also, explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...
Python question about creating classes, methods and objects. Class instance variables can be referenced using two different ways: either using the accessor methods or direct referencing by using the instance variable reference (for example to get the day of the date you can use self.d or self.getDay()). Which one abides more towards the principle of information hiding?
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...
Create a Business class: Instance variables: Student id 1000 - 9999 Student name Present Student email address Present Number of hours 3.5 - 18 Two constructors should be coded, one that accepts no arguments and sets every field to its default value, and one that accepts all four fields, and assigns the passed values into the instance variables. For each instance variable create two methods; a getter and a setter. Add a static method to the class that will accept...
Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods: A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables) A constructor that takes 3 parameters, one for each instance variable A mutator method called setHour which takes a single integer parameter. This method sets the value of...
1- Create the base class Book that has the following instance variables, constructor, and methods title ( String) isbn ( String) authors (String) publisher (String) edition ( int) published_year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method // that return sting representation of Book object. 2- Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title ( String) isbn...
In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...