Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

array initialization within class  RSS feed

 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I just started with Head First Java. I have finished a couple of chapters and it seems to be a fun book. I have a question relating to arrays. I will paste four code snippets. The first two do not work. I will also give the corresponding errors.

1)
class Temp {
String [] strArray = new String[3];
strArray[0] = new String();

public static void main (String [] args) {
}
}

Error:
Temp.java:3: ']' expected
strArray[0] = new String();
^
Temp.java:3: invalid method declaration; return type required
strArray[0] = new String();
^
2 errors

2)
import java.util.*;

class Temp {
String [] strArray = new String[3];

public static void main (String [] args) {
strArray[0] = new String();
}
}

Error:
Temp.java:5: non-static variable strArray cannot be referenced from a static context
strArray[0] = new String();
^
1 error

3)
import java.util.*;

class Temp {

public static void main (String [] args) {
String [] strArray = new String[3];
strArray[0] = new String();
}
}

4)
class Temp {
static String [] strArray = new String[3];

public static void main (String [] args) {
strArray[0] = new String();
}
}

The last two works. I have not yet reached the portion where it explains "static" keyword in detail. Any help will be greatly appreciated.

Imtiaz
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java, you can't 'do stuff' outside of a method. you can declare variables, and in some cases, declare and define them on one line, but nothing else.

in your first example, your first line inside the class says "every instance of this class will have a reference to an array of Strings." That is legal. You cannot try an create a String to put into the array. if you change it to this:
i think it will work.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example 2, you declare an array that will be a member of every instance of the class. until you actually create that instance, there is no array.

Your main method is static. That means it exists whether you create an instance of the object or not.

When you run your code, main() begins executing, and you have not created that object. so, you say "in that array that only exists once i've created the object, put something".

what you need to to is in main, create an instance of your class. Then, THAT reference has an array, and you can put something in it. so it would be something like:


Again, i'm not actually testing this, i'm just going off the top of my head.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that neither of these two have anything to do with arrays specifically. you would have the same problem with any class.


this should give you the same error as your case 1.
 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Fred. I certainly appreciate your help. I got your point - I do not have an object to modify its instance variables.

What about 3rd and 4th code snippets then. Although I now understand that they will never do what I expected, I am confused as to how they got compiled.

Also, I am a bit confused about static methods being able to execute without object being created, but I think I should get to this topic in Head First Java soon.

Imtiaz
[ May 20, 2007: Message edited by: Imtiaz Nizami ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are confused about in 3 and 4. in 3, you have a main method. inside that method, you create the array, and assign something to one of the slots.

In 4, you create a static array. That means the array exists ONCE, regardless of how many times the object it created, and every instance refers to that same array. if you change something in one instance, it will be reflected in all. it's kind of like a global variable.

Static basically means "this thing exists even if no objects are created". That is true for either methods or variables. that is the POINT of the word static.

think about it... if you didn't have such a thing, how would you ever execute a main() method?
 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point taken. Thank you very much Fred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!