• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stupid question about variables  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

bit confused about this...

if i have a class:


is private int x; before the method a class variable? what is it...?

i thought class variables and static variables were the same thing...preceded by key word static

i know to use the private int x; variable in a method you would need to use
this.x correct? as not to use a local variable instead.


 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry...please ignore static in the class...
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so its actually a class level variable
a static variable is something else... .this switches from using a local variable to a class level variable

though im not sure of the impact of class level vs local (the bigger picture)

i just googled and found this info

http://programmers.stackexchange.com/questions/293478/what-are-the-differences-between-class-variables-and-instance-variables-in-java

(Sorry guys, i know it must be a bit annoying posting my own questions and answers,but its just for my own future reference)
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private int x is an instance variable and to use it you can call it by this.x or just x.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so why does my instructor on a video call it a class level variable?

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
has anybody got a good tutorial that just breaks down all the variable definitions and where they would go in a program...i cant believe how confusing this is
i seem to read different things in different material
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:so why does my instructor on a video call it a class level variable?


Agreed with Ryan. In your example, x is an instance variable.

Henry
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:so why does my instructor on a video call it a class level variable?

Please tell us where the video comes from.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so is this right

instance variables same as member variables - not in any method (just inside the class) owned by the object,object has to be instantiated in order to reference,scope is the whole class

class variables same as static variables - must have keyword static,doesnt have to be instantiated,also defined in the class (not in any method) they apply to all instances of the class
so if you have private static int car = 2;

every object created from that class will have car = 2 available?

local and method variables are pretty straight forward.

is this right?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . instance variables same as member variables
No. Static variables are also members of the class. Don't say member variables. Say instance fields or static fields.
- not in any method (just inside the class) owned by the object,object has to be instantiated in order to reference,scope is the whole class

class variables same as static variables - must have keyword static,doesnt have to be instantiated,also defined in the class (not in any method) they apply to all instances of the class
so if you have private static int car = 2;

every object created from that class will have car = 2 available?
Yes.
local and method variables are pretty straight forward. . . .
No. They can be very confusing, so avoid local variables with the same name as fields except in simple set methods and constructors. Why are you calling something a method variable? That is not a normal Java® term. I presume you mean a parameter.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey campbell,

reading my wifes college notes...seems like the terminology they using is wrong

can you reco a good book i should start reading

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The amount of experience you have will determine the book you shou‍ld read. How much programming have you done? Have you come across our books pages.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!