• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question on classes,methods and objects  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reading this in my book:

"in addition to instance variables and methods,classes can also define class variables and class methods.You can access class variables and methods from an instance of the class or directly from a class--you dont have to instantiate a class to use its class variables and methods,Class methods can only operate on class variables--they do not have access to instance variables or instance methods."

so i have a couple of questions regarding this...

"in addition to instance variables and methods,classes can also define class variables and class methods."
i thought an object was instantiated when the new keyword was used...and the new object got its variables and methods from the class...what am i missing?

"You can access class variables and methods from an instance of the class or directly from a class--you dont have to instantiate a class to use its class variables and methods"
the first bit im ok with,its what i do all the time Aircraft spitFire = new Aircraft();
but directly from the class...i thought a class was just a blue print...and objects were made from classes.

"Class methods can only operate on class variables--they do not have access to instance variables or instance methods."
...lost here

thanks for reading guys


 
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

To give you some context, when your book is referring to "class variables" and  "class method", it is talking about static variables and static methods. Likewise, "instance variables" and "instance methods" are variables and methods that are not declared static.

Henry
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks hen
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"If you apply static keyword with any method, it is known as static method. A static method belongs to the class rather than object of a class. A static method invoked without the need for creating an instance of a class. static method can access static data member and can change the value of it."

i kind of understand this...but dont think i have a practical grasp on it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classic example is the Math class.  I don't really need to create a Math object to get the sine of pi/2.  So the method are made static, allowing me to use them without making the object.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks fred,

yes that made perfect sense...

i read this:

http://www.programmerinterview.com/index.php/c-cplusplus/whats-the-difference-between-a-class-variable-and-an-instance-variable/

would you agree?
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . i read this: . . . would you agree?
Note that is a C++ page, but it appears to be the same as in Java® except that in Java® those things are called fields, not member variables. They are members of the class. I like the way that link stresses that there is one copy of a static field.

Please always tell us the details of the book or other source, to avoid copyright problems.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!