Skip to content

Instantly share code, notes, and snippets.

@Deanout
Last active June 3, 2018 04:37
Show Gist options
  • Save Deanout/dbb63a17e5251aa563e85619f019f29c to your computer and use it in GitHub Desktop.
Save Deanout/dbb63a17e5251aa563e85619f019f29c to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author Dean
*/
public class ClassOne {
// Change this to public if you want the JavaApplication1 class
// to be able to access this object.
private ClassTwo classTwo;
public ClassOne() {
classTwo = new ClassTwo();
classTwo.sayHello();
}
public void callClassTwo() {
classTwo.sayHello();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author Dean
*/
public class ClassTwo {
public void sayHello() {
System.out.println("Hello!");
}
}
package javaapplication1;
/**
* @author Dean
*/
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// This will work just fine.
ClassOne classOne = new ClassOne();
// So will this:
classOne.callClassTwo();
// This Won't Work unless private ClassTwo is changed to public:
classOne.classTwo.sayHello();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment