Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Chuu
Sep 11, 2004

Grimey Drawer
Studying for the SCJP and have some syntax question.

1. Right now the following program prints "OHGOD" (as expected). How do I change it so it prints out "0 1 2 3" using the "x" variables defined in the nested classes?

code:
public class Nested{


    public static void main(String... args){
	TestClass.NestedTest.NestedNestedTest test = new TestClass().new NestedTest().new NestedNestedTest();

	test.printStuff();

    }
}

class TestClass{
    int x = 0;

    class NestedTest{
	int x = 1;
	
	class NestedNestedTest{
	    int x = 2;
	    
	    void printStuff(){
		int x = 3;
		System.out.println("OHGOD");
	    }
	}
    }
}
2. Is there a document somewhere that explains local inner classes and static nested classes clearly? The java tutorial explains Nested classes clearly but doesn't really go into local inner or static nested classes at all. Are static local inner classes legal? Are you allowed to add nested classes to local classes?

Chuu fucked around with this message at 15:07 on Aug 15, 2008

Adbot
ADBOT LOVES YOU

Chuu
Sep 11, 2004

Grimey Drawer

MEAT TREAT posted:

That's loving retarded, why would you ever want that.

Since your fields aren't private and they are all in the same package, I think you should be able to access each one's X by placing the class name dot x.

TestClass.x NestedTest.x NestedNestedTest.x and x

I agree that it's retarded, but a large part of the exam are things that you would never do in real life, but are there to see if you understand the language spec.

That example was pretty simple, I could make it more complex to stop you from "cheating," but what I really want to know is how to fully qualify names of shadowed data members. The only way I know how to do it is with "this" but that isn't enough with multiple-nested classes.

edit : Actually, if you don't think Anonymous classes are retarded, this sort of thing could happen really easily with Anonymous classes, in that you need a reference to an object that's not the top level or bottom level class.

Chuu fucked around with this message at 21:01 on Aug 15, 2008

Chuu
Sep 11, 2004

Grimey Drawer

Outlaw Programmer posted:

This will print out what you want (inside the printStuff() method)...:


Thanks a ton. That's the first use of Anonymous classes I've ever seen that actually seemed useful and not incredibly contrived.

Chuu
Sep 11, 2004

Grimey Drawer
Another syntax question! This time regarding class locks.

We have this code:

code:
class Lock1{

    public static void main(String[] args){
	Lock2 l2 = new Lock2();
	l2.counter();
	System.out.println("hi");
    }
}
Here are some different implementations of "Lock2." I'm a bit confused about them.

Implementation 1:

code:
class Lock2{
    static int count = 0;
    public static synchronized int counter(){
	return count++;
    }
}
No big deal. Checks to see if the class lock is available, since we only have 1 thread it is, excutes the code, prints "hi."

Implementation 2:

code:
class Lock2{
    static int count = 0;
    
    public static synchronized int counter(){
	synchronized(Lock2.class){
	    return count++;
	}
    }
}
According to the book I'm using to study, this should be equivalent to Implementation #1. Read naively, to me this means "Wait for the class lock before executing "counter()" then "Wait for the class lock before executing the return statement" i.e. it should automatically deadlock. Since it's functionally equivalent though, I assumed the "synchronized" keyword meant "somewhere in this method something is synchronized, not necessarily the entire method, unless you do not see a synchronized block, then the entire method is synchronized" i.e. it won't deadlock. Does what's expected, prints "hi". The problem is . . .

Implementation 3:
code:
class Lock2{
    static int count = 0;
    
    public static int counter(){
	synchronized(Lock2.class){
	    return count++;
	}
    }
}
This works too. So what exactly is the difference in #2 and #3, or is there something going on here that my trivial example isn't showing?

Chuu fucked around with this message at 12:48 on Aug 16, 2008

Chuu
Sep 11, 2004

Grimey Drawer
Ahh, ok, thanks a lot. I assumed they work the same was as mutexes on *nix.

To make sure, so if you have code like:

static synchronized void a(){...}
static synchronized void b(){...}

Any code which has control of the lock and executing a() will block any other threads from running b(), since the class level lock is associated with all static synchronized code in the entire class?

Chuu fucked around with this message at 17:20 on Aug 16, 2008

Adbot
ADBOT LOVES YOU

Chuu
Sep 11, 2004

Grimey Drawer
Can someone please tell me what current libraries are considered the best to interact with a SOAP web service? All I need to do is execute one request (although the response is likely to be 40+ gigs so I need to be able to treat it as a stream, and will be MTOM encoded). Hopefully very simple stuff.

I don't know the Java ecosystem at all, and considering how old Java is and how old SOAP is there are too many choices and I don't really have the background to filter it.

Chuu fucked around with this message at 06:45 on Oct 3, 2014

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply