import - Importing a Java custom class into another program -
i first year comp sci student , going through first round of textbook problems, dealing system.out.println method (no, im not looking on homework problems. i've satisfied (as far know right now) requirements of problem, looking gain information).
the first problem asked me write program output this:
////////////////////// || victory mine! || \\\\\\\\\\\\\\\\\\\\\\ this no problem. wrote following code:`
public class stewie { public static void main(string[] args) { line(); qoute(); line2(); } public static void line() { system.out.println("//////////////////////"); } public static void qoute() { system.out.println("|| victory mine! ||"); } public static void line2() { system.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); } }` later in text, asks me write program prints above figure 5 times in row. not problem, rewrote code first problem, this:
/* */ public class stewie2 { public static void main(string[] args){ newstewie(); newstewie(); newstewie(); newstewie(); newstewie(); } public static void newstewie() { line(); qoute(); line2(); } public static void line(){ system.out.println("//////////////////////"); } public static void qoute(){ system.out.println("|| victory mine! ||"); } public static void line2() { system.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); } } that's , well, i'd know how import class stewie first problem use in second 1 without having rewrite code. can me out??
edit:re:importing custom java class. saw before posting, not know enough programming helpful me @ moment. thank though.
have on how create , use packages.
because methods in first class static declared, can call them within second class.
package mypackage; public class stewie { public static void main(string[] args) { line(); qoute(); line2(); } public static void line() { system.out.println("//////////////////////"); } public static void qoute() { system.out.println("|| victory mine! ||"); } public static void line2() { system.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); } } the seconds class should in same package. then, because methods declared in first class static, can directly call them second class.
package mypackage; public class stewie2 { public static void main(string[] args) { stewie.line(); stewie.qoute(); stewie.line2(); } } now in second class, can extend code further functionality.
note: mentioned, allowed because methods static.
if method, bellow wasn't static:
public void qoute() { system.out.println("|| victory mine! ||"); } you following error message:
non-static method 'qoute()' cannot referenced static context.
so be-careful , notice difference.
Comments
Post a Comment