Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2857 > unrolled thread

how to access static instance variables defined in static initializer block

Started byMarc B <marc.at.compass@gmail.com>
First post2013-11-18 14:42 -0800
Last post2013-12-01 00:23 +0200
Articles 7 — 5 participants

Back to article view | Back to comp.lang.java.help


Contents

  how to access static instance variables defined in static initializer block Marc B <marc.at.compass@gmail.com> - 2013-11-18 14:42 -0800
    Re: how to access static instance variables defined in static initializer block Marc B <marc.at.compass@gmail.com> - 2013-11-18 14:51 -0800
      Re: how to access static instance variables defined in static initializer block Marc B <marc.at.compass@gmail.com> - 2013-11-18 15:01 -0800
        Re: how to access static instance variables defined in static initializer block markspace <markspace@nospam.nospam> - 2013-11-18 15:26 -0800
        Re: how to access static instance variables defined in static initializer block Lew <lewbloch@gmail.com> - 2013-11-27 16:05 -0800
    Re: how to access static instance variables defined in static initializer block Roedy Green <see_website@mindprod.com.invalid> - 2013-11-30 12:41 -0800
      Re: how to access static instance variables defined in static initializer block Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-12-01 00:23 +0200

#2857 — how to access static instance variables defined in static initializer block

FromMarc B <marc.at.compass@gmail.com>
Date2013-11-18 14:42 -0800
Subjecthow to access static instance variables defined in static initializer block
Message-ID<8ed39e6c-ccea-480e-b3a3-8b802957ede8@googlegroups.com>
Hello,

Preparing for the Java exam I have a question about static initializers.
A static initializer block can be used to initialize instance and static variables (http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). So I tried it out and it ended up with this:

//============================================================
package LearnJava;

public class Holder{
	// initialize instance varialbe
	{
		int int1 = 5;
		System.out.println("Initializing instance variable int1: " + int1);
	}
	// initialize static variable
	static {
		int sint1 = 6;
		System.out.println("Initializing static block. sint1: " + sint1);
	}
	// initialize instance varialbe
	{
		int int2 = 7;
		System.out.println("Initializing instance variable int2: " + int2);
	}
	// initialize static varialbe
	static {
		int sint2 = 8;
		System.out.println("Initializing static variable sint2: " + sint2);
	}
	
	int int1 = 5;
	
	public Holder(){
	}

	public static void main(String[] args){
		Holder h = new Holder();
		//System.out.println("Running main. sint1: " + Holder.sint1);
		System.out.println("Running main. int1: " + h.int1);
	}
}
//============================================================

I expect to be able to refer to int1, int2 and sint1 and sint2 from an instantiated class and - in the case of the static variables - from the class directly. But this class does not compile because of line
System.out.println("Running main. sint1: " + Holder.sint1).
Also System.out.println("Running main. sint1: " + h.sint1) doesn't compile. The error is:

 error: cannot find symbol
		System.out.println("Running main. sint1: " + Holder.sint1);
		                                                   ^
  symbol:   variable sint1
  location: class Holder
1 error

If I comment out this line it compiles and I get output 

Initializing static block. sint1: 6
Initializing static variable sint2: 8
Initializing instance variable int1: 5
Initializing instance variable int2: 7
Running main. int1: 5

Q:
Why is the instance variable in the static {} block suddenly unknown? And why can the instance variable declared in {} be resolved???
Does this mean the static variable scan only be initialized outside any block?

Related: When I put static int sint1 = 6; n ieither the constructor or main() I get an Netbeans error  "illegal start of expression" (the line gets underlined red and hovering over displays the error). Running te program gives a dialog "no main classes found"

Thanks,

Marc

[toc] | [next] | [standalone]


#2858

FromMarc B <marc.at.compass@gmail.com>
Date2013-11-18 14:51 -0800
Message-ID<ca817691-9fe0-41d4-8da0-8cb8fc7e1bf5@googlegroups.com>
In reply to#2857
Ok,. I found an answer..
I have to _declare_ te static variables outside te static initializer block and _initialize_ them in the static initializer 

static int sint2 = 8;
	static {
		sint2 = 8;
		System.out.println("Initializing static variable sint2: " + sint2);
	}

Then I can refer to Holder.sint2.

//

Still don't see why adding static int sint2 = 8; to contructor or main() doesn't work:

public Holder(){
	static int sint2 = 9;
}

Marc

[toc] | [prev] | [next] | [standalone]


#2859

FromMarc B <marc.at.compass@gmail.com>
Date2013-11-18 15:01 -0800
Message-ID<f01e9955-5cb7-4300-ace2-226a81b459a4@googlegroups.com>
In reply to#2858
Ok, found that answer too somewhere:

In Java, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects.

This means that static keyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.

So no 'static' keyword in any instance object like methods, just in the class initializer area as it is allocated during class creation, not instantiation.

Marc

[toc] | [prev] | [next] | [standalone]


#2860

Frommarkspace <markspace@nospam.nospam>
Date2013-11-18 15:26 -0800
Message-ID<l6e7nc$vqm$1@dont-email.me>
In reply to#2859
On 11/18/2013 3:01 PM, Marc B wrote:
>
> So no 'static' keyword in any instance object like methods, just in
> the class initializer area as it is allocated during class creation,
> not instantiation.


Well, and method declarations themselves.

public class Foo {

   public static void main( String... x ) {}
        //^^^^^^
}

Both fields (variables) and methods can be declared static (class scope).

Foo.main();         //  a valid method call.

Foo f = new foo();
f.main();           // valid but questionable, use Foo.main()

[toc] | [prev] | [next] | [standalone]


#2862

FromLew <lewbloch@gmail.com>
Date2013-11-27 16:05 -0800
Message-ID<7b31267e-3b11-434d-9f02-03eb95b7e11a@googlegroups.com>
In reply to#2859
On Monday, November 18, 2013 3:01:45 PM UTC-8, Marc B wrote:
> Ok, found that answer too somewhere:

You would have found it in the Java Tutorials. Did you read them?

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#2865

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-11-30 12:41 -0800
Message-ID<vajk991tpcum44tfchrkbccqmaumuqmpho@4ax.com>
In reply to#2857
On Mon, 18 Nov 2013 14:42:06 -0800 (PST), Marc B
<marc.at.compass@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>	static {
>		int sint1 =3D 6;

you still have to declare your variables static, even in a static
block
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Unlike many machines, computers require no water once they are
manufactured.

[toc] | [prev] | [next] | [standalone]


#2866

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2013-12-01 00:23 +0200
Message-ID<lviov934wd.fsf@saunalahti.fi>
In reply to#2865
Roedy Green <see_website@mindprod.com.invalid> writes:
> On Mon, 18 Nov 2013 14:42:06 -0800 (PST), Marc B

>>	static {
>>		int sint1 =3D 6;

> you still have to declare your variables static, even in a static
> block

What do you mean?
Variables defined inside a block don't exist outside that block, and
static modifier isn't even legal within a static block or a method.

-- 
Jukka Lahtinen

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web