0

I am a first year comp sci student and going through my first round of textbook problems, all dealing with the System.out.println method (no, im not looking for help on homework problems. i've satisfied (as far as i know right now) the requirements of the problem, am just looking to gain some extra information).

The first problem asked me to write a program that would output this:

//////////////////////
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\

This was no problem. I wrote the 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 is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}`

Later in the text, it asks me to write a program that prints the above figure 5 times in a row. This is also not a problem, I just rewrote the code from the first problem, like 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 is mine! ||");
}
public static void line2() {
  System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}        

That's all good and well, but I'd like to know how to import class Stewie from the first problem so I could use that in the second one without having to rewrite all the code. Can anyone help me out??

edit:re:importing a custom java class. I saw this before posting, but probably do not know enough about programming for it to be helpful to me at the moment. thank you though.

  • 1
    Possible duplicate of [Import a custom class in Java](http://stackoverflow.com/questions/7869006/import-a-custom-class-in-java) – Yassin Hajaj Jan 17 '16 at 00:19
  • Assuming that Stewie and Stewie2 are in the same package you can just type Stewie.line(); to call line function from Stewie class, since line is static declared. – Andrej Jan 17 '16 at 00:37
  • i've tried (admittedly, very half-assedly) to figure out what a package is/how to set one up, but it didn't make much sense. – Joshua Lee Jan 17 '16 at 01:05
  • I assumed that they are in the same package because by default every class that you create in one project are in the same package. There is nothing complicated about it, you can imagine packages like folders. So Stewie.line(); should work. – Andrej Jan 17 '16 at 11:24

1 Answers1

0

Have a look on how to create and use packages.

Because your methods in your first class are static declared, you can just call them within your 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 is mine! ||");
  }

  public static void line2() 
  {
    System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
  }
}

The seconds class should also be in the same package. Then, because your methods declared in the first class are static, you can directly call them to your second class.

package mypackage;
public class Stewie2 
{
  public static void main(String[] args)
  {
    Stewie.line();
    Stewie.qoute();
    Stewie.line2();
  }
}

Now in your second class, you can extend your code with further functionality.

Note: As already mentioned, this is allowed because your methods are static.

If your method, bellow wasn't static:

public void qoute() 
{
   System.out.println("|| Victory is mine! ||");
}

You would get the following error message:

Non-static method 'qoute()' cannot be referenced from a static context.

So be-careful and notice the difference.

Rafael
  • 7,002
  • 5
  • 43
  • 52