Skip to content

Instantly share code, notes, and snippets.

@daveshah
Created November 24, 2015 15:56
Show Gist options
  • Select an option

  • Save daveshah/23b32f00a227dc66238a to your computer and use it in GitHub Desktop.

Select an option

Save daveshah/23b32f00a227dc66238a to your computer and use it in GitHub Desktop.
Scoping Java objects - using package protected scope to ensure abstractions don't bleed out
package com.lolz;
public class AwesomeSauce {
{
// Note that the interface is package protected so trying to access these outside the
// package is no-bueno!
Foo.Bar bar = new Foo.Bar();
Foo.Baz baz = new Foo.Baz();
//THIS is the weird syntax I was talking about - "declaring 'new' off of an instance"
Foo.Bar.BarBazzer1 bazzer1 = bar.new BarBazzer1();
Foo.Bar.BarBazzer2 bazzer2 = bar.new BarBazzer2();
}
}
package com.lolz;
//Package level interface within Foo.java - This will provide package only access to the
// classes defined within the interface
interface Foo {
class Baz {
public void doTheBaz() {
}
}
class Bar {
class BarBazzer1 {
void doTheBazzer() {
}
}
class BarBazzer2 {
void doTheBazzer() {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment