• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling method on field, how it is possible?  RSS feed

 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I understand that method is called by instance of class (ie, Object), and cannot be called by field (ie, variables).

But I look at System.out.println()
System is a final class, out is a static final field, and Println() is a public method that be called by a field.

I seem don't understand this, is that possible in Java when method being called by a field?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By calling,I think you mean invoking,

So tell me how you represent an object, in java codes.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:So tell me how you represent an object, in java codes.


Do you mean create a new object?

This is,


Yes, calling I mean invoking.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here MyClass's instance's refernce is stored in objMyClass,which is indeed a variable, and you are using that variable to invoke the method

Normally we use reference variable, which stores the reference of the object, and we invoke method with help of that
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you meant to say when a method is called on a field. That is easy enough to achieve:-Look at line 13. I hope you are not superstitious. You have a field (list) and you are calling a method (get) on it. You need to pass information (i) which tells you which element you are looking for (and then print that element). As you know, too, if you pass a number outwith the range of real elements you will suffer an Exception.

It is conceivable that there is some method in a field which takes the object it is a field of and calls methods on that, but that looks to me like circular logic, so you are unlikely to be able to find a straightforward example to demonstrate such occurrences.

What the System class does is to hold methods and fields which are specifically related to the computer you are using and the runtime you are running. System.in is an example of an input‑output stream which deals with input from the “standard input”. It is actually an object which the System class/runtime creates to implement that standard input. You can call methods on it (e.g. read() but if you use read once, you will never want to use it again). You can also pass System.in to constructors of other classes, e.g.
new Scanner(System.in)
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ST is right to point out references, and System.in is really a reference to an object which implements standard input.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There have been a lot of good comments while I was writing this, but I'll post it anyway.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Thompson wrote:

So, in your example, what is 'objMyClass', if not a field?

Also: couldn't line 4 be
  System.out.println( "My Name:" + myName.toLowerCase() );
instead?

Winston
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, just to add a little more icing, perhaps it would help to call a "field" by its proper name: instance variable. And then to recognize that what you've been calling a "field" is also a variable, albeit one that belongs to an object instance, and so exhibits the same behavior as variables.
 
Hai Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sirs,

You guys are very kind. Thank you very much.

I think my problem is that I think "out is not an object but primitive variables", and I think that all method should be call (invoke) by only reference type variables (right?).

For example,

So if out is an object, the method println() invoked on it is easy to understand for me now.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've got it, except that out is a static reference to the type PrintStream which contains the method println(), but you can think of it as a reference to an object -- just that it's a reference to a class. There is no instance.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to confuse you in this,but to make you more clear

See reference variables and objects (instances) are two separate things

Think it like this: reference variable are like remote to the object,which helps in directing the object what method of its is needed to be executed
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Thompson wrote:. . . I think "out is not an object but primitive variables", . . .
No, System.out is not a primitive. It is a reference to an object, more precisely a PrintStream object which is created to work as the “standard output” for the runtime you happen to be using. You can find more details by looking in the API documentation for System.out and other links there.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I think you've got it, except that out is a static reference to the type PrintStream which contains the method println(), but you can think of it as a reference to an object -- just that it's a reference to a class. There is no instance.


I'm not too sure I understand what you're getting at here.
The last bit ("There is no instance") seems to imply that out is not an instance of anything...when it is. It's an instance of PrintStream.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it wrong. Sorry for the confusion.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I got it wrong. Sorry for the confusion.


Oh that's OK...thought I was having a Friday Moment.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!