• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fields, methods and constructors  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there!

Ok, might be a little bit silly question but it really bothers me. How do exactly fields and methods interact with each other within the same class? I mean, here's an example code:


how does that exactly work? I mean I need to 'call' these methods from the 'outside' of a class (or object) in order for them to actually be of any particular use, right? It's sort of like 'skills' that given object will inherit from it's 'parent' (class)? Do i understand it correctly?

And other thing how exactly do constructors work? How do they determinate the way objects are being created? What 'ways' there might be?

Cheers!
K
 
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kamila,

From inside of a class, a class has access to any of its fields. With other classes, it depends on whether you declare that field public, private, or protected (I won't cover protected at this point, but you can check out an example here.) Let's assume you had another class called AddressBook. AddressBook wants to store the ContactInfo for Jane Doe. If first name was a public field, AddressBook could simply assign the first name by doing the following:

While this might seem like an easy solution, it's generally considered safer to set all of your fields to private in order to help protect them from unwanted outside access. If firstName was declared private, AddressBook would not be able to directly set the value of firstName. Instead, it could use the public "setter" method, setFirst:

Constructors work by creating a given object in memory based off of a class. A very basic constructor would look as follows, using your ContactInfo class:

A default constructor that passes no arguments, such as the one above, would create an empty object. But let's say you wanted to assign certain values to a Contact when you create a new one, such as first and last name. In order to do this we can set up the following in your ContactInfo class:

You can then call this code in another class, such as the AddressBook class I mentioned, by writing it as the following:

This creates a new ContactInfo object, named contact, with the firstName and lastName fields set to Jane and Doe, respectively.

Hope this helps!
 
Saloon Keeper
Posts: 7994
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kamila,

Yeah, you can consider methods as "skills", if that makes things easier. They usually operate on properties that are specific to one object: the object's values of the class' fields.

What you're seeing in the code you posted are "getters" and "setters". These may make the distinction between fields and methods more unclear. They are used so you can expose information about an object, without exposing the fields directly. Fields should almost always be declared as private.

Here's another example:

You can see that this class allows you to play a game of Tic Tac Toe, and all the methods either get information about the private grid field, or they edit it. It also has a constructor that makes sure that the grid is in a valid state when you create a new object. When you call the line TicTacToe game = new TicTacToe(); it is guaranteed that the grid field of the new object will be 3x3 and have no Marks in it.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are no silly questions.
The code you posted unfortunately won't compile as-is, and I've added some code-tags to make it more readable.
In your example the ContactInfo class declares a member variable firstName, which should probably be declared private.
This is an instance variable, meaning every instance of this class - every ContactInfo object - gets its own copy.
You can call the setFirst() method of that object to assign a value to the firstName variable and the getFirst() method to retrieve it.

Here what that could look like:




Edit: little late to the party
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys a lot for all of your responses (there's no such thing like late to the party)!

No to be a pain or anything, but I want to make sure that I am clear on the topic. So:

1. fields (understandably), as a rule we make privet so no one messes with our data.
2. each and every object has unlimited access to fields/methods within itself (so it can mess with it as much as we want it to)
3. we set methods mainly so we don't have to expose fields and therefore avoid making too many errors and code too complicated.
4. 'protected' data is a bit higher level than such a greenhorn as me. ;-)

So as a summary object within itself has no limits, we can use and abuse each and every method or field.
Objects among each other communicate through public methods that we classify as 'setters' and 'getters'.

 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:2. each and every object has unlimited access to fields/methods within itself (so it can mess with it as much as we want it to)

This is not exactly true. Every class has access to all its fields and methods. This means that class X can access the members on object B from object A. Here's an example:


Objects among each other communicate through public methods that we classify as 'setters' and 'getters'.

This is misleading. Objects communicate through invoking methods on other objects. Whether they're setters or getters is irrelevant. There are methods other than setters and getters.
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is not exactly true. Every class has access to all its fields and methods. This means that class X can access the members on object B from object A.


Would you care to elaborate bit more?
 
Stephan van Hulst
Saloon Keeper
Posts: 7994
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The access modifiers don't have anything to do with objects. They work in the context of source files. A private member may only be accessed from the same source file. A member with default access may only be accessed by source files in the same package.
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think I got it. :-)
Ta!
 
Sheriff
Posts: 4935
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila Bertran wrote:Would you care to elaborate bit more?
I believe you understood correctly in your above post. Stephan just used more accurate and correct terminology.
That each instance of particular class has access to all its members as well as methods of another instance of the same class directly. Well, probably Stephan's explanation is more clear.

Based on his example (hopefully he'll correct me if I interpreted wrongly what he meant):
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, in the compareTo method specified by the Comparable interface, the Foo object has access to a private field of the other Foo object.
 
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this might be helpful for you to understand the constructor as well as the concept that we can access data member of one object by another object
this is actually the concept of "Copy constructor"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!