Results 1 to 7 of 7

Thread: instance vs static members

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    instance vs static members

    Im trying to illustrate uses of instance vs static field members.
    Just for myself to see how they work, and i think i have done this succesfully. But is there a way i can (for the sake of code reusability) only create one copy of get and set methods that i have defined in my classes, so i dont have to define them for every class? Would an interface be the logical way to go?

    Code:
    /*
      this is a test to see if a static member is diffrent from an 
      instance member. 
    */ 
     public class Test{
         public static void main(String[] args){
          
           Car NSX = new Car();
           Boat Fountain = new Boat();
          // show defalults 
           System.out.println("Max mph for your car is" + NSX.getMaxMPH());
           System.out.println("Max rpm for your car is" +  NSX.getMaxRPM());
           System.out.println(); 
           System.out.println("Max mph for your boat is" + Fountain.getMaxMPH());
           System.out.println("Max rpm for your boat is" +  Fountain.getMaxRPM()); 
           System.out.println();
    
           NSX.setMaxRPM(9000); // set new RMP for Boat also, since redline is shared among 
            // all instances; 
    
           System.out.println("Max mph for your car is" + NSX.getMaxMPH());
           System.out.println("Max rpm for your car is" +  NSX.getMaxRPM());
           System.out.println(); 
           System.out.println("Max mph for your boat is" + Fountain.getMaxMPH());
           System.out.println("Max rpm for your boat is" +  Fountain.getMaxRPM()); 
           System.out.println();
    
           NSX.setMaxMPH(230); // just set MPH for car since mph is an instance member.        
    
           System.out.println("Max mph for your car is" + NSX.getMaxMPH());
           System.out.println("Max rpm for your car is" +  NSX.getMaxRPM());
           System.out.println(); 
           System.out.println("Max mph for your boat is" + Fountain.getMaxMPH());
           System.out.println("Max rpm for your boat is" +  Fountain.getMaxRPM()); 
           System.out.println();
    
             }  
       }
    
       class Vehicle{
         protected  int mph; 
         protected static int redline;
        
         public Vehicle(int mph, int redline){
            this.mph = mph;
            this.redline = redline; 
          } 
        }
    
     class Car extends Vehicle{
       public Car(int mph, int redline){
         super(mph,redline);
           this.mph = mph;
           this.redline = redline;     
        }
        public Car(){
         this(200,6500); // defualts
         }
        public int getMaxRPM (){
             return redline;  
         }
        public int getMaxMPH(){
           return mph;
         }
        public void setMaxRPM(int setRPM){
            this.redline = setRPM; 
        } 
        public void setMaxMPH(int setMPH){
            this.mph = setMPH;  
        } 
    }
    
    
     class Boat extends Vehicle{
       Boat(int mph, int redline){
         super(mph,redline);
           this.mph = mph;
           this.redline = redline;      
        }
       public Boat(){
         this(120,6500); // defualts
         }
       public int getMaxRPM(){
             return redline;  
         }
       public int getMaxMPH(){
           return mph;
         } 
      public void setMaxRPM(int setRPM){
            this.redline = setRPM; 
        } 
        public void setMaxMPH(int setMPH){
            this.mph = setMPH;  
        }      
    }

  2. #2
    Lively Member
    Join Date
    Mar 2001
    Location
    Massachusetts, USA
    Posts
    111
    or an abstract class

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    mmmmm yes that is a thought. But if i use
    an abstract class or an interface
    I have to provide an implemetation of
    every method declaration within the interface or
    my class will become abstract itself. So ill have
    to think about what to do.

  4. #4
    Member
    Join Date
    Aug 2001
    Posts
    50
    try declaring those functions as final functions

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Which means that they would be unable to be overridden.
    I dont see how that would help me with code reuseability
    But i fyou care to elaborate it would be helpfull

  6. #6
    Member
    Join Date
    Aug 2001
    Posts
    50
    I think i have misunderstood ur question.Could u pls rephrase it?

  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I just wanted to see if there is a way in which i dont have to declare those two methods multiple times for each class. But i guess the only way to do that is declare one set in the Vehicle class which is the parent or "Super class" and declare the methods with the static keyword so all objects share a copy of those two methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width