0 Members and 4 Guests are viewing this topic.
public class Main extends JApplet { public static void main(String[] args) { MyFunction(); //This won't work } public void MyFunction() { System.out.println("Hello World"); }}
public static void MyFunction(){}
ToUpperCase myToUpperCaseFunction = new ToUpperCase();
window.setContentPane(new Main());
public class MainClass { public static void main(String[] args) { MyFunction myToUpperCaseFunction = new MyFunction(); MyFunction(); } public void MyFunction() { System.out.println("Hello"); } }
Of course that won't work. Because MyFunction needs to be a class. What you need is this:public class MyFunction { public static void main(String[] args) { MyFunction myToUpperCaseFunction = new MyFunction(); // Create a new instance of this class called MyFunction. I.E. calls the constructer. } public MyFunction() { // This is what is called when you call the "constructer" with no arguments. (Nothing inside the parenthesis) Notice that there is no return type, as you are creating and returning an "instance" of the object MyFunction. System.out.println("Object initiated"); } }
That's actually not entirely correct. You can have many main functions, but only one is defined as THE main function that is called.
ClassName objectName = new ClassName(<param list>);(yes, i know you can do ClassName objectName = new SubClassName(), i'm keeping it simple.)
public class MainClass { public static void main(String[] args) { MainClass myToUpperCaseFunction = new MainClass(); MyFunction(); } public void MyFunction() { System.out.println("Hello"); }}
^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();
Quote from: graphmastur on March 07, 2011, 06:38:46 pm^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a function
myToUpperCaseFunction.MyFunction();
Quote from: nemo on March 07, 2011, 06:52:03 pmQuote from: graphmastur on March 07, 2011, 06:38:46 pm^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a functionit doesn't have to have class in it at all. If he wanted the class to be MyFunction, he should call it MyFunction, not MyClass. a method is a function, yes. But in this case it would be a method of the instance variable. e.g.:Code: [Select]myToUpperCaseFunction.MyFunction();
Quote from: graphmastur on March 07, 2011, 06:53:35 pmQuote from: nemo on March 07, 2011, 06:52:03 pmQuote from: graphmastur on March 07, 2011, 06:38:46 pm^ Yes, that's another way. The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a functionit doesn't have to have class in it at all. If he wanted the class to be MyFunction, he should call it MyFunction, not MyClass. a method is a function, yes. But in this case it would be a method of the instance variable. e.g.:Code: [Select]myToUpperCaseFunction.MyFunction();yes, that's what i was aiming for. i just realized MyFunction isn't static so my code won't compile, but regardless if i saw a class called MyFunction i would be confused.
import javax.swing.*; public class HelloWorldSwing { /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("HelloWorldSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add the ubiquitous "Hello World" label. JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }}
public class MainClass { public static void main(String[] args) { MainClass mainClass = new MainClass(); mainClass.MyFunction(); } public void MyFunction() { System.out.println("Hello"); }}
I'm making my class an object and accessing one of its properties, which is MyFunction() right?
public class Car{ public String make; public String model; public boolean working; // Set up the variables of the class. public Car(String arg1, String arg2) // A constructor; it gets called when a new Car object is created to set up some of the new Car's variables. { this.make = arg1; this.model = arg2; this.working = true; } public void dispMakeModel() { if (this.working) // First check if it works or not. { System.out.println("This car seems to be a " + this.make + " " + this.model + "."); } else { System.out.println("This car is broken! Fix immediately!"); } } public void drive() { this.working = false; // Car no longer works. System.out.println("CRAAASH!!!11"); }}
public static void main(String[] args){ Car myPrecious = new Car("Ford", "Fusion"); // Creates ("buys") a new car with make "Ford" and model "Fusion". Notice that there are two arguments, because the Car constructor above takes two arguments. myPrecious.dispMakeModel(); // Displays the string "This car seems to be a Ford Fusion." myPrecious.drive(); // Take it out for a drive. Oh darn, you crashed. myPrecious.dispMakeModel(); // This time it displays "This car is broken! Fix immediately!"}