Results 1 to 3 of 3
  1. #1
    daveabes is offline Member
    Join Date
    Feb 2014
    Rep Power

    Default Non Static Variable cannot be referenced?

    Hi guys. Very new so trying out a few exersizes. Im getting a syntax error saying I cannot reference a non static variable from a static perspective.
    How this is happening is with my variables declared in the beggining. When outputting those variables in my last statement, I am not allowed to.
    The source is below. Could anyone share some clarity on this issue?


  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power

    Default Re: Non Static Variable cannot be referenced?

    main is static, your totalCreditHours variable is not. And, it would be zero at this point, from what I can see, anyway.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Rep Power

    Default Re: Non Static Variable cannot be referenced?


    maybe you need some explanation, what static means:

    Static things (variables, functions) exist on the class level. The non static elements exist on the object level.

    So in your class Fees, each object of a Fee has their own version of the 4 variables you defined there.
    The static function main exists inside the class. (Some people say, that all objects of a class share the static things, but I like to see it as something that is not connected to the instances at all.)

    So when you want to access something non static, you need an instance of the class. But you do not have any instance of the class Fees anywhere. So you cannot access it.

    Hope people get not mad at all, but I do a car comparison again:
    - you can have a plan how a car is build. On that plan you can write something. But if you write on a plan "call wife", then it is something that is just on the plan. That is nothing that the cars will have at all.
    - The description of the plan could describe that each car build by this plan will have a trunk. But with just the plan you cannot put anything into the trunk. To put somehint in the trunk, you need a real instance of that car. And of course: each car will have its own trunk.

    So the plan is the class in this comparison. And the build cars will be instances of that class. The trunk is a simply non static variable.
    And now you just have on the plan something like "Get the luggage from the trunk" and the compiler simply tells you: Sorry, there is no car!".

    I hope that I was able to help you with my description and that I didn't confuse you.

    With kind regards,


Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 01:46 AM
  2. Replies: 3
    Last Post: 01-24-2013, 07:20 PM
  3. Replies: 7
    Last Post: 12-07-2012, 02:00 PM
  4. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

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