YesNoOk
avatar

JAVA programming - what are instances and constructors? (Read 1416 times)

Started by He-Man, April 16, 2008, 09:54:47 pm
Share this topic:
JAVA programming - what are instances and constructors?
#1  April 16, 2008, 09:54:47 pm
  • avatar
  • ***
Heyo folks :)

I am trying to learn some JAVA stuff now - and I am into Object oriented programming.
Some things keep bothering me though, might be because of my English.

1.) When I read about object oriented programming, I keep hearing "is an instance of"...
Now, what does that mean? What does instance mean anyway? I tried Googleing stuff, but
it didn't really provide me a "solid" answer.
From what I understand - instances are objects of classes. But I am very unsure about it.

2.) And constructors? What are they used for anyway and when do I need to put them in?
To best of what I know - it's like making an object out of a class, which is later used inside another class that did make the constructor. And the reason for making a constructor is to actually be able to use the constructing class as an object. That's as far as I come - as I said - I don't know. Could anyone please specify?



System.out.println("Thanks in advance!!"); //.... :P
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.
Re: JAVA programming - what are instances and constructors?
#2  April 16, 2008, 11:43:17 pm
  • ******
  • [E]
    • Mexico
Re-read a book, your idea of instances is almost ok, but what you say about constructors seems to be completely off.
Re: JAVA programming - what are instances and constructors?
#3  April 17, 2008, 01:02:45 am
  • ******
    • Germany
Quote
1.) When I read about object oriented programming, I keep hearing "is an instance of"...
Now, what does that mean? What does instance mean anyway? I tried Googleing stuff, but
it didn't really provide me a "solid" answer.
From what I understand - instances are objects of classes. But I am very unsure about it.

When refering to an "object", one usually means an "instance of a class". This is easiest explained by example.

A "class" is the structure of an entity, the class "member" on this forum would have attributes (in java called "fields") like name, postcount, avatar, and methods like "edit" and "delete". An instance of this class (an "object") would be your and my account.

Quote
2.) And constructors? What are they used for anyway and when do I need to put them in?
To best of what I know - it's like making an object out of a class, which is later used inside another class that did make the constructor. And the reason for making a constructor is to actually be able to use the constructing class as an object. That's as far as I come - as I said - I don't know. Could anyone please specify?

A constructor is a method called upon instantiation of a class. To stick to our example, the constructor of the member class would be called upon registration, and would be used to initialize all the fields for the object (getting a member id, setting initial values (like postcount to zero), and so on).
Re: JAVA programming - what are instances and constructors?
#4  April 17, 2008, 03:25:11 am
  • ******
  • In after lock
    • mugenguild.com/~messatsu/index.html
As a beginner, you'd get more solid answers from a beginners textbook.  You can go to the library and read it for free even!


Many people risk their lives everyday by having Mugen.
Re: JAVA programming - what are instances and constructors?
#5  April 17, 2008, 10:54:02 am
  • avatar
  • ***
As a beginner, you'd get more solid answers from a beginners textbook.  You can go to the library and read it for free even!

LoL. I have teh book, but I didn't understand it that well. That's why I asked. I read, wikipedia'd and google'd this. Couldn't get it. So - I asked here. :P
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.
Re: JAVA programming - what are instances and constructors?
#6  April 18, 2008, 05:23:19 pm
  • *****
  • can see your halo
    • Germany
    • Skype - panchasell
    • www.mugenguild.com/
But did you get it now or would you like more assistance??
"Several times now, Achamian thought he had glimpsed golden haloes about Kellhus's hands. He found himself envying those, such as Proyas, who claimed to see them all the time."
--R. Scott Bakker
The Thousandfold Thought (2006)
Re: JAVA programming - what are instances and constructors?
#7  April 21, 2008, 08:48:18 pm
  • avatar
  • ***
But did you get it now or would you like more assistance??

Sorry. I was away for a time. But I think I understand it more now. Really. I think.
I've been reading both Valodim's examples (thanks Valodim:) ), the one's in my book and online after time, after time.
I think I kind of understand it.

Constructor's are similar to methods, but are made inside a class which you want to make object off.
This constructor's are later called, inside the main class for example, so you can make objects of them - i.e, you make instantination of the classes you want to make objects of.

So "instantinate a class" is the same as saying: "make an object of a class."

The variables you use to reference to the object then, is just a reference.
And the variables should be called instance variables, if I recall it correctly.
They are no real values, but rather a pointer which points to a certain part of the computer memory which contains the values of that object, if I understood it correctly.

So for example:

Point b = new Point(34, 36);

the "new" operator creates an object, of the type "Point". It is nameless, but the type (with its parameters) are stored somewhere in the memory, and the variable b will be a reference to that location.

Point b = new Point(34, 36);
Point c = new Point(34, 36);

Variable b and variable c are not the same thing. There are two different locations in the memory and each variable point to each one of them. Even though the values are the same, the refering variables are not. They point to completely different parts of the memory - but these parts do contain same value. But they're not the same.

Point b = new Point(34, 36);
Point c;
c = b;

Now, here - the references c and b are the same. Since the reference of object b is copied to c.

If I have any error in what I have typed, please tell me.
I think I understand it well now. I have a little hard time to explain though. What do you think? :)
The key to happiness is not to mourn on the past, or to worry about tomorrow - but to live here and now. In the present. Honestly and earnestly.
Re: JAVA programming - what are instances and constructors?
#8  April 21, 2008, 08:54:10 pm
  • ******
    • Germany
Yeah sounds like you got it. :)

That is also the difference between == and the equals method. Example:
String one = "stuff";
String two = "stuff";
if(one == two)
  this_is_false();
if(one.equals(two))
  this_is_true();

In many other languages (C and PHP, for instance), the == operator compares the values of the variables, in Java the objects themselves are compared for equality.