Question

Java Homework Question: Explain how class (static) variables and methods differ from their instance counterparts. Give...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor or block.

  • As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
  • Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
  • Initilisation of Instance Variable is not Mandatory. Its default value is 0
  • Instance Variable can be accessed only by creating objects.
  • Static Variables: Static variables are also known as Class variables.
  • These variables are declared similarly as instance variables, the difference is that static variables are declared using the static keyword within a class outside any method constructor or block.
  • Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.
  • Static variables are created at the start of program execution and destroyed automatically when execution ends.
  • Initilisation of Static Variable is not Mandatory. Its default value is 0
  • If we access the static variable like Instance variable (through an object), the compiler will show the warning message and it won’t halt the program. The compiler will replace the object name to class name automatically.
  • If we access the static variable without the class name, Compiler will automatically append the class name.
  • Each object will have its own copy of instance variable whereas We can only have one copy of a static variable per class irrespective of how many objects we create.
  • Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of instance variable. In case of static, changes will be reflected in other objects as static variables are common to all object of a class.
  • We can access instance variables through object references and Static Variables can be accessed directly using class name,
  •   

    Live Demo

    import java.io.*;
    public class Employee {
    
       // this instance variable is visible for any child class.
       public String name;
    
       // salary  variable is visible in Employee class only.
       private double salary;
    
       // The name variable is assigned in the constructor.
       public Employee (String empName) {
          name = empName;
       }
    
       // The salary variable is assigned a value.
       public void setSalary(double empSal) {
          salary = empSal;
       }
    
       // This method prints the employee details.
       public void printEmp() {
          System.out.println("name  : " + name );
          System.out.println("salary :" + salary);
       }
    
       public static void main(String args[]) {
          Employee empOne = new Employee("Ransika");
          empOne.setSalary(1000);
          empOne.printEmp();
       }
    
  • }

Add a comment
Know the answer?
Add Answer to:
Java Homework Question: Explain how class (static) variables and methods differ from their instance counterparts. Give...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...

    JAVA Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (float) and color of the pen (String). 2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    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...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    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...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Can you please produce the Java code that solves this non-assessed homework problem? I'm very new...

    Can you please produce the Java code that solves this non-assessed homework problem? I'm very new to Java and Object-Oriented Programming and I have no idea how to construct the logic for this code. PROBLEM: "In this exercise, you will build a program that determines the validity of a select few simple English sentences, according to rules detailed below. A sentence will be represented as an array of words, which you will create as a static nested class `Word`. `Word`...

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • * Create a Single Java Class that contains two static methods that solve the specified problems...

    * Create a Single Java Class that contains two static methods that solve the specified problems on slides 3 and 4. Call the static methods with the parameters specified in the Task. ifu Submit as single java file Submit to Blackboard by 8AM on March sth. Assignment is worth 2 pts Cannot be late. . . Remember you should create/use 'helper' methods to solve your problem rather than creating a single lengthy method. However, all'helper' methods must be called from...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java 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 - 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 constructor (set...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • What is exception propagation? Give an example of a class that contains at least two methods,...

    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.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT