Created
September 29, 2023 17:15
-
-
Save CodeByAidan/4aa6b51af0240f9ed74c494f36b5567f to your computer and use it in GitHub Desktop.
This Java code demonstrates basic object-oriented programming principles. It includes a `Main` class with methods to set and display object properties. The `Helper` class showcases object interaction, highlighting encapsulation and method usage.
This file contains hidden or 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
| public class Main { | |
| int x; | |
| public Main(int x) { | |
| this.x = x; | |
| } | |
| public Main() { | |
| this(10); | |
| } | |
| public void foo() { | |
| Helper.doSomethingWith(this, 42); | |
| } | |
| public void setX(int x) { | |
| this.x = x; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Main Object with x=" + x; | |
| } | |
| public static void main(String[] args) { | |
| Main myObj = new Main(5); | |
| myObj.foo(); | |
| } | |
| } | |
| class Helper { | |
| public static void doSomethingWith(Main mainObj, int newX) { | |
| System.out.println("Original object: " + mainObj); | |
| mainObj.setX(newX); | |
| System.out.println("Object after changing x: " + mainObj); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment