Reputation: 43
I'm struggling to call for a function that is located in a different package than my main class. The function I'm talking about is the one below:
wtchPrdct = createWatch(keyboard);
It was located within my main function to create a Watch object like below
Watch wtchPrdct = null;
Because my program became quite long as I was adding stuff to it, I wanted to take out some weight from my main class, and thus, I've created a package 'Utils' to which I've added my createWatch() function. However I struggle to call that function now. I get an 'error cannot find symbol' if I stick with my original line :
wtchPrdct = createWatch(keyboard);
And change the way I cal my method as such:
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);
but unfortunately got the same error.
I've forgot to mention that I've imported my package:
import Utils.Create;
package SportswearProduct;
import DBClass.Model;
import java.sql.SQLException;
import java.util.*;
import java.sql.Date;
import Utils.Create;
public class SSD_CA2 {
public static void main(String[] args) throws SQLException {
System.out.println("\n================== MENU ==================");
do {
/* the user may choose beetwen CRUD interactions provided by the program */
System.out.println("\n1. Create a new Sportswear");
System.out.println("2. Read Watch");
System.out.println("3. Update Watch");
System.out.println("4. Delete Watch");
System.out.println("5. Exit");
System.out.print("\nEnter option: ");
/* optCommand interprets as a string the command from the user, parse it as an Integer
* & checks whether the command matches one of the CRUD interactions below */
String optCommand = keyboard.nextLine();
opt = Integer.parseInt(optCommand);
// The program prompts a command feedback
System.out.println("\nYou chose option " + opt);
// I use a do-while loop that iterates until the user prompts a correct command (see option 5)
switch (opt) {
case 1: {
System.out.println("\nWhat product would you like to add?");
System.out.println("1. A Watch");
System.out.println("2. Runners");
System.out.print("\nEnter option: ");
String createCmd = keyboard.nextLine();
int newOpt = Integer.parseInt(createCmd);
do {
switch (newOpt) {
case 1: {
System.out.println("\n================== Create Watch ==================");
/* spwrPrdct will store the data of an object watch created in the function createWatch to which we pass
* as a parameter the method Scanner() */
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);
// once returned, prdctWatch id passed to the addWatch() function in Model.java
model.addWatch(wtchPrdct);
break;
}
case 2: {
//same process as above
System.out.println("\n================== Create Runners ==================");
runPrdct = createRunners(keyboard);
model.addRunners(runPrdct);
break;
}
default:
if (opt > 2) {
/* if the user inputs a value greater than 5, instead of stoping, the program prompts an error message
* & iterates again */
System.out.println("\n================== ERROR ==================");
System.out.println("\nWrong command! select one or 2 :");
}
}
break;
} while (newOpt != 1 || newOpt != 2);
}
}
}
And here is my function:
package Utils;
import SportswearProduct.Sportswear;
import SportswearProduct.Runners;
import SportswearProduct.Watch;
import java.sql.Date;
import java.util.Scanner;
public class Create {
Watch wtchPrdct = null;
/* -------------------- CREATE WATCH FUNCTION -------------------- */
/* createWatch() passes Keyboard()so that the user may assign data to the object */
public static Watch createWatch(Scanner keyboard) {
/* the user enters data that is stored into a variable of matching datat type */
System.out.print("\nEnter the watch brand: ");
String brand = keyboard.nextLine();
System.out.print("Enter the date of sale (yyyy-mm-dd): ");
Date onSale = Date.valueOf(keyboard.nextLine());
...
Watch prdctWatch = new Watch(
brand,
onSale,
price,
movement,
chargingType,
batteryLife,
waterProof
);
return prdctWatch;
I cut the last part as it works and don't want to overwhelm the code.
Thanks for your help.
Upvotes: 0
Views: 1547
Reputation: 825
Just an information don't try to create object from Utils method, make them with private constructor. In this case you can create an object from the Utils class but can't use that method since it's static. Remember that the static methods belong to the class and not to the object. For that reason use it like this:
Create.createWatch(keyboard);
And check the access modifiers. Make them public so you will be able to access them, check if you maybe have protected or not at all modifier.
Upvotes: 0
Reputation: 441
I'm confused a bit. Is this question really about java? Basically function in java can not be independent and can not belong package. Function should belong either class(static) or object.
However, if we still talk about java, I guessing Utils is separate class, like:
public class Utils {
public Watch createWatch(Object keyboard) {
// something important happens here
}
}
if this is the case - createWatch belongs to any instance of Utils. So to call it, you need to create instance first:
new Utils().createWatch(keyboard);
or
Utils u = new Utils();
u.createWatch(keyboard);
For utility purposes, you can use static
public class Utils {
public static Watch createWatch(Object keyboard) {
// something important happens here
}
}
in this case it's part of class not object:
Utils.createWatch(keyboard);
or you can call it directly with static import
package main;
import static utils.Utils.*;
public class Main {
Object keyboard = new Object();
public void someMainMethod() {
createWatch(this.keyboard);
}
}
Upvotes: 0
Reputation: 8873
Well you have made some fundamental mistakes here in the code.
Take is code part for example:
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);
If you wanted to call the createWatch()
function in the Utils.Create
class, you must to it like
Create.createWatch(keyboard)
Now, next problem is that you are returning the type Watch
from the createWatch()
method. So you cannot assign it to wtchPrdct
variable, as its type is Create
So the correct call will be like
Watch wtchPrdct = Create.createWatch(keyboard);
Now if you really want to call the method createWatch
without Create
type, you can import the function statically like
import static Utils.Create.createWatch.
Type matters in java. So the return values must belong to the correct type.
Upvotes: 0
Reputation: 361
I think you have confused the grammar of Java and scripting languages like Pyhon.
Since your method createWatch
is declared in the class Create
, when you want to invoke that method you should:
Either invoke that method by using an instance of class Create
:
wtchPrdct = wtchPrdct.createWatch(keyboard);
or invoke that method by setting the method static and invoke it by using class name:
wtchPrdct = Create.createWatch(keyboard);
Upvotes: 1