Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By dlorde

Thread: Initializing objects- nonstatic fields cannot be referenced from a static context

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Initializing objects- nonstatic fields cannot be referenced from a static context

    The (values of) fields of a particular object are assigned to the nonstatic fields of the class via constructor, when the object is initialized. Now what if I want to read in the values of these fields of the objects from the user?

    For example Bicycle is a class with nonstatic fields viz. gear and speed. Let mountainBike an object of bicycle. Now, when this object is created, it has to be initialized by some values of gear and speed. And I have to read in the values from the user.

    Now, when i try to do that by invoking methods, which return the values for these fields of the objects by taking the input from the (keyboard) user, after the object is created; it gives an error on the statement which creates the object i.e.
    Java Code:
    Bicycle mountainBike= new Bicycle(speed, gear);
    .
    The error states that:

    " Nonstatic variables speed and gear cannot be referenced from a static context"

    What is meant by static context? Plus what is the solution now?
    Following is the code of the program.


    Java Code:
    package practiceconstructor;
    
    import javax.swing.JOptionPane;
    
    class Bicycle{
    int speed;
    int gear;
    
    Bicycle( int speed, int gear){
    this.speed=speed;
    this.gear=gear;
    }
    
    int getGear(){
        
        String g= JOptionPane.showInputDialog(this,"Enter speed:");
        int currentGear = Integer.parseInt(g);
    return currentGear;
     }
    
    int getSpeed(){
    String s= JOptionPane.showInputDialog(this,"Enter speed:");
    int currentSpeed= Integer.parseInt(s);
    return currentSpeed;
    }
    
    public static void main(String[] args){
    Bicycle mountainBike= new Bicycle(speed, gear);//ERRONEOUS
    int speed= mountainBike. getSpeed();
    int gear= mountainBike.getGear();
    System.out.println("Speed:" + speed + "Gear:" + gear);
    }
    }
    Please clarify my concept. I shall be grateful.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are using the speed and gear variables in a static method, the main method, but these variables aren't static and thus cannot be accessed that way. If you really needed to access these variables, you'd need to do so by referencing them on a Bicycle object. But this is moot since you really don't want to access them this way. Instead create some variables local to the main method, fill them with values before using them and then use them as parameters for your Bicycle constructor call.

  3. #3
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    Static context means that you are trying to call/reference something from within a static method, in this case your main method. Your problem is happening because of two things. The first is that you have to declare variables before you reference them. The second is that you haven't named your variables well, which is leading to confusion.

    Java Code:
    class Bicycle{
    [COLOR="lime"]int speed;
    int gear;[/COLOR]
    
    Bicycle( int speed, int gear){
    this.speed=speed;
    this.gear=gear;
    }
    
    int getGear(){
        
        String g= JOptionPane.showInputDialog(this,"Enter speed:");
        int currentGear = Integer.parseInt(g);
    return currentGear;
     }
    
    int getSpeed(){
    String s= JOptionPane.showInputDialog(this,"Enter speed:");
    int currentSpeed= Integer.parseInt(s);
    return currentSpeed;
    }
    
    public static void main(String[] args){
    Bicycle mountainBike= new Bicycle([COLOR="lime"]speed, gear[/COLOR]);
    int [COLOR="red"]speed[/COLOR]= mountainBike. getSpeed();
    int [COLOR="red"]gear[/COLOR]= mountainBike.getGear();
    When you are calling 'Bicycle mountainBike= new Bicycle(speed, gear)' speed and gear are referring to the variables that you declared outside of the method(highlighted in green) not the variables below it (in red), which is why you are getting your error. To access the variables speed and gear (the ones in red), you have to declare them before you intialize the new bicycle object.

    one way to fix this is to declare the new Bicycle object but not initialize it, then declare the speed and gear variables and then intialize the object.
    Java Code:
    Bicycle mountainBike = null;
    int speed= mountainBike. getSpeed();
    int gear= mountainBike. getGear();
    mountainBike= new Bicycle(speed, gear);
    I think that the best way to do this would be to call the getSpeed and getGear methods from the contructor of the object.
    Java Code:
    Bicycle(){
    this.speed= getSpeed();
    this.gear= getGear();
    }
    And then create a new object like
    Java Code:
    Bicycle mountainBike= new Bicycle();

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mr_guy View Post
    I think that the best way to do this would be to call the getSpeed and getGear methods from the contructor of the object.
    Java Code:
    Bicycle(){
    this.speed= getSpeed();
    this.gear= getGear();
    }
    And then create a new object like
    Java Code:
    Bicycle mountainBike= new Bicycle();
    Are you sure about this? You'd be calling getSpeed() and getGear() before they have been initialized which doesn't smell right to me.

    Rather why not leave the constructor as is since there is no error in the Bicycle constructor, and simply call it in the main method with two int numbers or two local variables that have been initialized?

    To the original poster: I strongly recommend that you not to follow mr guy's advice.
    Last edited by Fubarable; 07-19-2011 at 02:11 PM.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    There is a problem with the Bicycle class design - the getSpeed() and getGear() methods should, by convention, return the Bicycle's current speed and gear. Instead, they prompt for user input. So mr_guy's suggestion, at best, would cause two user prompts for input every time a new Bicycle is created - not good if you want to create a number of Bicycles and read their speeds and gears from, for example, a file.

    A better design would have the user input external to the Bicycle class, so the Bicycle is not reliant on user input and could be passed speed and gear from any source (database, user, another class, etc).

    So I would prompt for user input of gear and speed separately, and then create the Bicycle, passing the gear and speed to the constructor. Alternatively, user input could be dropped altogether, and the speed and gear values passed to the constructor as literals (hard-coded values).
    Tolls likes this.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 4
    Last Post: 04-16-2011, 06:15 PM
  3. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Tags for this Thread

Posting Permissions

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