Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Created September 29, 2023 17:15
Show Gist options
  • Select an option

  • Save CodeByAidan/4aa6b51af0240f9ed74c494f36b5567f to your computer and use it in GitHub Desktop.

Select an option

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.
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