class Coordinates2D {
int x;
int y;
}
// Verwendung
c1 = new Coordinates2D();
c1.x = 5;
c1.y = 1;
Objekt wird zuerst erzeugt, dann werden die Attribute initialisiert
class Coordinates2D {
int x;
int y;
Coordinates2D(int pX, int pY) {
x = pX;
y = pY;
}
}
// Verwendung
c1 = new Coordinates2D(5, 1);
thisstaticpublic class Coordinates2D {
private int x;
private int y;
public Coordinates2D(int pX, int pY) {
x = pX;
y = pY;
}
}
private: Nur aus Objekten der selben Klasse sichtbarpublic: Keine Einschränkung der Sichtbarkeitpublic class Shadowing {
private String s = "Attribute";
public void printString(String s) {
System.out.println(s);
}
static public void main(String[] args) {
new Shadowing().printString("Parameter");
}
}
public class Coordinates2D {
private int x;
private int y;
public Coordinates2D(int x, int y) {
this.x = x;
this.y = y;
}
}
this bezieht sich immer auf das aktuelle Objektthis verwendetpublic class Coordinates2D {
private int x;
private int y;
private static int count = 0;
public Coordinates2D(int x, int y) {
this.x = x;
this.y = y;
count++;
}
public static int getCount() { return count; }
}
// In the meantime somewhere else
Coordinates2D.getCount();
public class Poser {
public String getShowOffString(String noun) {
return String.format("Look at my %1$s, my %1$s is amazing!", noun);
}
}
public class Coordinates2D {
private int x;
private int y;
// Constructor goes here
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
// Getter and Setter for y go here
}
void steht für „Kein Rückgabewert“public class Coordinates2D {
private int x;
private int y;
public Coordinates2D(int x, int y) {
this.x = x;
this.y = y;
}
public Coordinates2D() {
this(0, 0);
}
}
public class FatOrangeCat {
private String lastMeal;
public void eat(String food) {
lastMeal = food;
}
public void eat() {
lastMeal = "Lasagna";
}
}
public class DoesNotMatter {
public static void main(String[] args) {
// Method body here
}
}
class Date {
byte day;
byte month;
short year;
}
class Author {
String firstName;
String lastName;
Date birthday;
}
class Document {
String title;
java.util.Locale language;
Date publicationDate;
Author author;
}
toString() Methode für alle Klassenjava.util.Locale