• Post Reply Bookmark Topic Watch Topic
  • New Topic

An array in a constructor  RSS feed

 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

I have been playing around with classes and instances of classes.

In this example I have one class which takes in 2 strings and a char.
The second class has a string 2 integers and an array of class 1, for example, test.
It contains a print method and a couple of others but no setters.
All the variables are private.

It runs and prints out everything I ask it to do which is great.

My question is:
Because I have no setters must I assign the values into each array field in this class?
Is it possible to assign values in the driver class as it is?
I didn't think so but just need clarity if you have the time?
I tried assigning values in the driver class just to see and it ignored them. I think I understand why?

The array is set at 5 and the values of the array are assigned within the constructor.

I know there is another way....

Instead of doing that I could just have a constructor with only the variables declared in this class in the constructor class without having an array object in the constructor.
Then in the driver class I could create a new instance of the array and of the 2nd class and assign the values there. I am not using scanner just yet as I wanted to keep it simple. Again no setters here either.
I do think that understanding all of this is massive.

Thanks once again
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass an array to a constructorTry that. See how far you get with it. See whether it actually compiles
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need an import: import java.util.Arrays;
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell,

Many thanks for the exercise.

I have tried to compile and run it but it doesn't yet - giving errors.

In ArrayDemoDemo it doesnt like this line:

ad = new ArrayDemo(8437, 27364, 8347, 20943, 0);

In ArrayDemo it doesn't like the the second constructor.

I can play around with these myself.

Just wondering though in the ArrayDemo class you are returning Arrays.toString[numbers]. It doesnt like that either as it doesnt seem to recognnise the word Arrays. What is it exactly? I do get the gist of what you are trying to do though.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I forgot about that. Delete the second constructor altogether and change the go method to this:-I have changed lines 8 9 10 11.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologise Campbell - my code is always full of mistakes anyway :-).

The result is the same as mine except that I assigned my values in the first class.

Would you mind if I looked at it for a while and tried to tell you the answer?

I notice no set methods either and the declaration of the instance of the array as final. So I'll do a bit of reading and playing around and see what happens?
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I chose those features with a reason: to lull you into a false sense of security about the ArrayDemo class being immutable. You will have seen that it isn't from the first two outputs, but using clone() breaks the link to the included array. You can read about arrays in the Java Language Specification, but it may be difficult to read.

I shall leave it to you to work out how to make ArrayDemo really immutable.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stupidly left out the clone line first time around. I have never used clone before but I see what it does now.
Sometimes certain knowledge at the wrong time can be dangerous but I think it's good to know.

I thought that the use of the word final meant that the value of the variable could never be changed but if fairness I haven't used either final or static variables just yet?

Softly, softly catches the money if you know what I mean?
If I become an overloaded constructor at this stage I'll probably just blow up :-)

So another question...

Does it mean that even with setters, once you declare numbers as final that you cannot add any more elements to the array?

Even leaving out final, isn't the correct way to go to use getters and setters in the demo class and then in the driver class(DemoDemo) to first populate the array and then use your setter method should you want to modify it?

Am moving on to inheritance shortly and see if I can understand the basics. It looks fairly straight forward.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read about clone()?
If you search you may be able to find a sample chapter from Effective Java (EJ) by Joshua Bloch. You want the old edition sample (hard to find), and it tells you about clone(). Alternatively, your College Library should have EJ on the shelf. If they haven't got it, chivvy them until they buy a copy.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Kellytt wrote: . . . inheritance . . . It looks fairly straight forward.


It isn't
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final means you always use the same array, but arrays are implicitly mutable, so anybody with a reference to that array can change its state.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell,

I'm not in college, I'm studying Java at home myself for myself and do not have a tutor or teacher as such. After Christmas I'll be doing a brief Java course but it is only for the work experience that goes with it. J2ee is where the jobs are I think so I will need to study that after I finish this. Sorry for giving the wrong info regarding the delimiter switches. I meant well.

Thank you also for explaining final to me.

I will research clone over the coming weeks but have moved on to inheritace now. I needed a change. Maybe it is tough but it seems far easier to grasp than classes and instances?

Anyway I want to conquer it all and be an excellent java developer in time and that means making mistakes, ripping it up and starting again if needs be. To every problem there is a positive solution. Sometimes I look at an exercise and go blank before convincing myself that it has to be related to what I have studied or have done before. Dissecting it and breaking it up is the key but I have a long way to go.


Your advice as always is brilliant and invaluable. Should you wish to confuse me with an inheritance problem in a couple of days please feel free to do so.

Brian

 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have only explained part of what final means.
Yes, writing code, deleting it and starting from scratch is a good way to learn.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks :-)
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
I think you need to understand basic Java (J2SE) before trying JEE.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely, I couldn't agree with you more.

I am not even writing proper programs yet, just merely playing around with code and concepts in an attempt to understand what is happening.

It is easier for a camel to enter the eye of a needle than for a person to enter the kingdom of Java :-).
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Kellytt wrote: . . . It is easier for a camel to enter the eye of a needle than for a person to enter the kingdom of Java :-).
No, it isn't, but programming is difficult. One of the mistakes we see here is that people overthink what they are doing, and that makes both the process and the code they create much more complicated than they ought to be.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Campbell the reason for that is lack of understanding.

I myself am not ready to undertake anything substantial just yet, the reason being that I would still find it extremely difficult to break it down into smaller simpler units. However that will happen in time provided I keep practising and reading.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean. That is the sort of thing this website is good for.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!