• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor for arrays  RSS feed

 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

I'm wonder about the issue of constructor for arrays. Let say I have a class tablica, and one
component int[] tab. If I get it right until now tab is nothing more than empty reference to some unexisting array?



Then, I'm trying to build the constructor for class tablica.
What is the proper form of it?
What can be the parameter of such constructor? Is it fields of array?

It is simple forf for basic variable
- I liken values defined in constructor with those global defined in class. But how to do it with array component tab.

If I create array object in main method then how can I use this constructor?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this programme compiled? I dont think so.

There are several issues:

1) You are using array with declaring it. You have not provide the size of an array.
2) Class Name should start from the Capital.
3) Problem is also with "ret_urn" method.

There are some more problems. I haven't checked deeply.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote: . . .
1) You are using array with declaring it. You have not provide the size of an array.
. . .
I cannot understand that bit. OP is declaring the array, but not instantiating it, so if you ever get rid of the many compiler errors and try to assign to one of the array elements in line 16, you are dealing with the default value for a field (=null) and that will cause an Exception.

Before using the array, it must be assigned to, and you can read about creating arrays in the Java® Tutorials. You may find an initialiser (what that Java® Tutorials link calls “shortcut syntax”) easier to use.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote: . . .
There are some more problems. I haven't checked deeply.
Too true there are problems. I do not like ret_urn; it looks dreadful to divide a keyword with an underscore like that. At least that (like the name of the class) is only a style problem which the compiler won't notice.

More serious problems are that you are displaying something in a method which ought to return an array. A method should do one thing and one thing only. Also there is a problem in the loop at line 10, that it will run for ever.
The fact that x is not declared before line 10 or that you are not returning something from the method in line 24 or that you have the wrong name for an array in line 23 (write tab not tab[]) are very minor problems … because the compiler can be relied on to find those problems and stop you running the code at all.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have actually written two valid constructors. The real problem is that neither of them instantiates the array reference before it is used.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Przemek Geminski wrote: . . . . . .
Please tell us what the largest possible value and the smallest possible value for any of those array elements is.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand that bit

Sorry for the confusion. I want to tell the OP that s/he using array without instantiation. So line numbers 13, 17,18,19 gives compilation error.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I thought you meant, but you wrote with rather than without.
No, the compiler errors do not occur at lines 12/13, 17, 18 and 19. Those lines will suffer runtime errors, if you are lucky throwing Exceptions rather than going into an infinite loop.
The compiler errors occur in different places.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup at runtime not compile time. Thanks for correcting again..
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I cleaned up a bit and now first generation class generates array as I want. The problem arises with Inheritance of constructor and the reference to the array (which is the only component of first-generation class).
So, the upper clas is



and then the lower class:



// ....
// and some following

[/code]

The first what I hear from compiler is:

TablicaIntowaZKopiowaniem.java:7: error: call to super must be first statement i
n constructor
super(x);

Well, in my case it IS the first statment in constructor?! so, what am I doing wrong?


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Common error: that is not a constructor. You must not give a return type, not even void.
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great! I fixed as you said and no previous error returned. but... there is a new issue: why in the subclass I can not use the Tab component (which is the only component of parent class) in any method? there is a error message that

.\TablicaIntowaZKopiowaniem.java:14: error: non-static variable Tab cannot be re
ferenced from a static context
czyIstnieje = Tab != null;
^(Tab is pointed)
.\TablicaIntowaZKopiowaniem.java:17: error: non-static variable Tab cannot be re
ferenced from a static context
System.out.println(Arrays.toString(Tab));
^(Tab is pointed)

while I successfully used this reference in methods of parent class. Is it inherited or not the heck?! If so, why I can't use it in the way I do it in upper class?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not the error you are getting, but it is still an error to use a superclass field in the subclass.
Give the int[] private access in the superclass. Write methods which use that array, all in the superclass. Write constructors which set up the array and initialise it to a real value. Get rid of the TabInitiate method. There are three reasons for that:-
  • 1: That code ought to be in the constructor (probably).
  • 2: The method is not called in any code that I have seen. That means the array will remain {0, 0, 0, ...} forever.
  • 3: A public method can be called from outside the class and the state of the array can be changed at any time without your object being in control.
  • Now all you need to do to extend the class is write a class with constructors. There is no need for any methods unless you need to override them.

    Remember: all methods which use the array are in the superclass. No new methods in the subclass (probably).

    The error message you showed says that you are trying to access the array from a static method. That is not possible.
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    well, I applied your hints and it worked again also in subclass ;) thanks

    now my constructor is:


    But, is it the only way to create a proper constructor for array?
    What if I want the length of array to be a parameter of constructor (like I done in the previous example)?
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Przemek Geminski wrote:
    But, is it the only way to create a proper constructor for array?
    What if I want the length of array to be a parameter of constructor (like I done in the previous example)?


    In your previous example, you didn't declare any constructors. Your example had methods (with the same name as the class). Note that constructors do not have a return type.

    Henry
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes, true, I wrongly did some return type. Campbell Ritchie also pointed that. But then I removed void and it still desn't work.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Przemek Geminski wrote:yes, true, I wrongly did some return type. Campbell Ritchie also pointed that. But then I removed void and it still desn't work.


    ... which you never showed us. And obviously, arrays (or any data type) can be initialized in constructors, so, something else is wrong. We don't have enough information to answer your inquiry.

    Henry
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7991
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your subclass as well? Post the entire code and explain what's not working exactly.
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, I wrote the code again from the scratch and it is working also with this parametrized constructor as well with Random initialization. I don't know where was the mistake before. Probably somewhere in further class. thanks for help



    I


    II.


    III.



    IV.



    cheers
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    and for your curious here is the previous version, that dysfunctional one:

    I.

    II.

    III.

    IV.



    And below the report from compiler during try of TablicaZKopiowaniemTester class:

    .\TablicaIntowaZKopiowaniem.java:14: error: Tab has private access in TablicaInt
    owa
    czyIstnieje = Tab != null;
    ^
    .\TablicaIntowaZKopiowaniem.java:17: error: Tab has private access in TablicaInt
    owa
    System.out.println(Arrays.toString(Tab));
    ^
    2 errors

    cheers
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7991
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Congratulations!

    It seems your showTab() was not working, because it's a static method and it's referring to a non-static member Tab. Tab is also private, so can only be referenced from within its class, and not outside classes.

    There are two tips I would like to give to you for your future programs. Please start all variable names with a lower-case letter. Identifiers that start with a capital letter are usually assumed to be type names. The second piece of advice is to code in English. It may suck to give up some national identity, but it's usually worth it when you're looking for help online, or when people have to maintain your code.

    Good luck!
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for further guidance. I will apply it.

    I changed also the static in the showTab() method, as well as private to public in Tab variable. And indeed - no errors returned during compilation. But then, when I tried to run the tester I got:

    X:\PG\study\tablicaintowa\II>java TablicaIntowaZKopiowaniemTester
    Error: Could not find or load main class TablicaIntowaZKopiowaniemTester

    Anyway, if I can't make the variables private in the upper class then what should I do? Or maybe the Tab should appear in constructor of subclass in more explicit way?
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Przemek Geminski wrote:
    Anyway, if I can't make the variables private in the upper class then what should I do? Or maybe the Tab should appear in constructor of subclass in more explicit way?


    Private variables can't be accessed by other classes -- even by subclasses. That is why it is "private". If you want the variable accessed by subclasses, perhaps declaring it as "protected" would be acceptable. Alternatively, you can keep it private, but provide getters/setters with the correct checks (and authentication) that you want.

    Henry
     
    Przemek Geminski
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok, I got it. I just somehow mistaken the meaning of the private and protected. thanks
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!