File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem with super in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with super in Java" Watch "Problem with super in Java" New topic

Problem with super in Java

Cento e Cem

Joined: Dec 22, 2012
Posts: 1
Hi folks:

I have a problem with a tiny Java program.

The problem is:

Giving an array of ints (with possible duplicate elements) I want to list pairs of numbers in ascending order without repetitions.

The classes "Sequence" and "FixedSize" can't be changed, because they are inherited by other classes (external to this code) and they ran without errors.

The only class that may (and need to) be changed is "SortArray".

Here's the code:

When we ran the code snippet:

The expected output should be:

0:1 - 1:2 - 2:5 - 3:8 - - 0:2 - 1:5 (two sequences of numbers - one for each while - in ascending order without repetitions)

However this code doesn't run, because the static array "WithoutRepetitions[]" being static is shared by the two instances of Sequence (s1 and s2),
and both while loops show the second sequence (0:2 - 1:5) crashing the program in the first while because the length of s1 is bigger than the length of s2.

If I exchange the order of instructions like below, it works:

In that order it works, however I'm not allowded to do this:

Somebody advice me to instead of a static array, I must use an instance array, however this is not possible, because the method "RemoveDups" must be static
to be passed as a parameter of super() and because of this constraint (static) can't access non static variables.

Can someone help me to solve this ambiguity? This is a kind of "deadlock" problem I can't solve.

Thank you in advance for your help.

Tony Docherty

Joined: Aug 07, 2007
Posts: 2839
Please use Java naming conventions ie methods and variables start with lowercase letters.

because the method "RemoveDups" must be static
to be passed as a parameter of super()

You aren't passing RemoveDups as a parameter to super(), you are passing the value returned by RemoveDups.
I haven't time to look at your code and fully understand what you are trying to do but something feels wrong about the design here. However a simple way around your problem would be to have a static method that just counts and returns the number of unique values in the array and use that to pass the size to super() and then have RemoveDups() as an instance method which you call after calling super().
I agree. Here's the link:
subject: Problem with super in Java
It's not a secret anymore!