int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error!
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
System.out.println("How are you?");
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
}
System.out.println("continue");
}
}
public class MyClass {
static void myMethod() {
// code to be executed
}
}
public class MyClass {
static void myMethod() {
System.out.println("I am your function.");
}
public static void main(String[] args) {
System.out.println("Hello World");
myMethod();
System.out.println("near end");
}
}
public class second {
int x = 5;
}
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
second myObj = new second();
System.out.println(myObj.x); // 5
}
}
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
second myObj = new second();
myObj.x=40;
System.out.println(myObj.x); //40
}
}
public class second {
static void myMethod() {
System.out.println("Hello World from second file!");
}
}
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
second.myMethod();
}
}
public class second {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
}
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
second.myStaticMethod(); // Call the static method
//second.myPublicMethod(); This would compile an error
second myObj = new second(); // Create an object of MyClass
myObj.myPublicMethod(); // Call the public method on the object
}
}
// Create a Car class
public class Car {
// Create a fullThrottle() method
public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}
// Create a speed() method and add a parameter
public void speed(int maxSpeed) {
System.out.println("Max speed is: " + maxSpeed);
}
// Inside main, call the methods on the myCar object
public static void main(String[] args) {
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}
// The car is going as fast as it can!
// Max speed is: 200