I have the classes created, I'm trying to
FilePostProcessFactory PostProcessFactory = new FilePostProcessFactory();
FilePostProcess filePostProcess = PostProcessFactory.getFilePostProcessName(fileName);
filePostProcess.getFileConfig(fileId, postProcessInstructions);
This method: getFileConfig is giving me an error: is not public in packageName'. Cannot be accessed from outside package
I was reading this:
https://www.javatpoint.com/factory-method-design-pattern
and they have implemented there that the abstract void getRate(); can be accessed from another class outside of the package.
What am I missing?
Thank you
Change the package of a class from which you're calling it to be the same as the package of the target class, then the error will dissappear. You could also use reflection, and call setAccesible(true) on the method reference, then invoke it.
Try declaring the class public or do what Krzysztof mentioned.
I ended up renaming the class: public abstract class myClassName{...}
That fixed it
Related
I am having trouble with a method that can be accessed using the full class path, but if I try and import it and call the method, it says that method is not defined for the current class. Here is a screenshot of the issue:
"The method equalTo(int) is undefined for the type App"
If you wish to call the method like the line 7 of your screenshot indicates, you would need to use static import:
import static org.hamcrest.Matchers.equalTo;
This will allow you to call the method as you seem to need:
equalTo(5);
import static org.hamcrest.Matchers.*;
you need to define static imports. The equalTo(T) is a static method.
Or you can access it with class name like:
Matchers.equalTo(5);
You can't access a method directly. You need to specify the class that has this method.
You should change your import to "org.harmcrest.Matchers"
And use: "Matchers.equalTo(5);"
Unless, of course, you use "import static", like the others users have shown. :)
I have 2 packages, airline and userInterface
In the AirportMain I am trying to create an instance of class AirlineReservation but it is from package userInterface, unlike my AirportMain.
I'm using:
AirlineReservation airlineReservation = new AirlineReservation();
but getting an error that it is not public in AirlineReservation so it cannot be accessed outside the package, but I do have:
public class AirlineReservation {
in class AirlineReservation.
What am I doing wrong here?
If the constructor for AirlineReservation is not declared public, that is your problem there. Make the constructor public.
If it is public, it's possible you need to put
import userInterface.AirlineReservation;
at the beginning of AirportMain.
Make the constructor public and for more information about access modifier link.
I've been working on some problems from Project Euler, and, in the process, have written a lot of useful methods (in Java) that I might like to use in other Java projects. I want to be able to call them in the way that you call a function from java.lang.math, so if I had a method primeFactor() I could call it using MyMathMethods.primeFactor(number). How would I go about this? Would I make some kind of package that I could import? Would I make a superclass that includes all my useful math-y functions and have whatever class I'm working with in a new project extend that? There are probably multiple ways to do this, but I don't know what is best. Thanks in advance.
Mark your utility methods as public static. Package your classes containing those utility methods in a jar. Add/Refer that jar in your project, where you want to use the. Then in your code you can call them in a static way lke : MyUtilityClass.myUtilityMethod();
The best thing for this situation is to work in meaningful packages and make their jar
You can create a package like
/* File name : Animal.java */
package animals;
interface Animal {
public void eat();
public void travel();
}
Also on classes
package animals;
/* File name : MammalInt.java */
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
You can import them like
import animals.*; OR be more specific import animals.MammalInt;Now you can make the jar file , import it in your project and use its methodYou can eaisly do it by this commandjar cmf MyJar.jar Manifest.txt MyPackage/*.class
For more details about jar creation please see thisAs a side note: Be carefull about visibility of members and functions while packaging itBecause there usage and accessibility matters a lot while we are using them
You could create separate java project with your util classes only and then create jar file and import into any another project.
Simply instantiate the class. Like your example, if you had a class MyMathMethods with the function primeFactor(number) then at other classes, simply instantiate it with something like private MyMathMethods myMathMethods;. Now, to call the function simply do myMathMethods.primeFactor(number); You may need to import its package as well.
False understanding of packages is any class defined within a package is visible to all other classes. Not true from my experience. If you have classes containing utility style methods you want to make available in another class? Simply declare a new instance of the class in the class you need the method in. Like... private MathUtilsClass mathUtilsClass = new MathUtilsClass(): Then any method you want to call from this class uses the new identifier, e.g. mathUtilsClass.greatFunction(); This is stupidly easy and should solve your problem.
I am trying to instantiate a Java abstract class from my Groovy code. Considering the following Java abstract class (non relevant processing is stripped out from the class):
public abstract class StackOverflow{
public abstract String answerMe();
}
I can easily instantiate it in Groovy this way, and the call to answerMe() will trigger the correct output:
StackOverflow stack = [answerMe : { "Answer" }] as StackOverflow
Now if I modify the StackOverflow class adding a String parameter in the constructor like this :
public abstract class StackOverflowStr{
public StackOverflowStr(String s){}
public abstract String answerMe();
}
I don't really know how to instantiate my object, I tried a lot of thing, but I can't seem to find the right syntax, does someone got any clue ?
You can instantiate it in classic Java style:
StackOverflowStr stack = new StackOverflowStr("javaish"){
String answerMe() {"answer"}
}
Just for the record, and to be clear on wording: in all of these scenarios, you're not instantiating an abstract class.
Abstract classes are classes that can never be instantiated.
You're instantiating a concrete anonymous class that extends an abstract class. B-)
I have a strange problem i don't know if i miss something. Here is my code
public interface Book{
}
public class MyBook implements Book
{
}
public static void main(String[] args)
{
Book b = new MyBook(); // compiler error: Type mismatch ....
}
Can somebody explains to me is this really a compiler error or just my eclipse is acting weird?
Thanks,
Your main method is not in a class, try putting it inside a class.
Also make sure to have only one public class per Java file.
I think after implementation of interface you can make object of class that implemented interface so make the object of class "MyBook"
Sorry, there is another interface of exact same name that is in the imported statement that is causing the problem. Thanks.