Last active
June 3, 2018 04:37
-
-
Save Deanout/dbb63a17e5251aa563e85619f019f29c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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