-5

I was writing a piece of code that goes like this,

public class Grades 
{
   public int marks; // what's the purpose of this? 
...
...
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Kal
  • 1
  • 2
    What do you mean? Why have any variables? Why is it int? Why is it public? Why is it plural "marks" yet only a single value instead of an array or List? – duffymo Aug 24 '15 at 00:32
  • 2
    Read your textbook please – Kon Aug 24 '15 at 00:32
  • 1
    The line of code that you mention it, defines a variable named 'marks', the variable type is Integer (int) and the scope of the variable is public. – SkyMaster Aug 24 '15 at 00:38
  • Potential duplicate: http://stackoverflow.com/questions/16686488/java-what-is-an-instance-variable – Marc Baumbach Aug 24 '15 at 00:48

2 Answers2

0

By declaring the variable outside of any methods you've made it an attribute of the class. This means that any method in the class can access it, and depending on its encapsulation (public/private/protected/package private) other classes can access it as well.

As for that specific variable's purpose in that specific class, that cannot be determined without seeing more code.

Zarwan
  • 5,537
  • 4
  • 30
  • 48
  • Not global; publicly available. I would say public and static would be global, since there's only one class instance in memory. – duffymo Aug 24 '15 at 00:45
  • @duffymo you're right, I thought they were called global in general, but that's only one case. I'm not sure if publicly available is correct either though, isn't that assuming that it's public? – Zarwan Aug 24 '15 at 00:48
0

Objects are data and methods encapsulated together into a single software component.

Classes are templates ("cookie cutters") from which you can create one or more instances in memory ("cookies"). Each one is independent; each can have its own state.

duffymo
  • 305,152
  • 44
  • 369
  • 561