Calling Methods Error - java

I keep getting the error cannot find symbol. Symbol: Method countAll. However, how i have it set up my professor keeps insisting this is how to have it set up.
import javax.swing.*;
import java.io.*;
import java. util.*;
public class Homework15{
public static void main(String args[]){
try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File text = chooser.getSelectedFile();
LetterCount.countAll(text);
}
catch (IOException error) {
System.out.println(error);
}
}
}

Since you are using a static method called countAll from the class LetterCount, you need to import such class, like this:
import your.package.LetterCount;
where your.package is the package where that class is located

LetterCount sounds like a utility class with static methods inside so you probably haven't imported the correct packages on top from from it looks like. Go to the class and on top copy the package path and import it in the class with the main method.
Also make sure that the methods are actually static.

As Juxhin and Jmm said: make sure to import the packages. If that doesn't work make sure your countAll method is not private. Because if it is private you won't be able to access it outside of the LetterCount class.

Related

Java import wildcard accessibility for nested static classes

How does the java accessibility (or perhaps, scope) work with respect to type import multi-level nested classes? An example:
ClassA.java:
package com.oracle.javatests;
public class ClassA {
public static class NestedAA {
public void printSomething() {
System.out.println("inside " + this.getClass().getName());
}
public static class NestedAB{
public void printSomethingAB() {
System.out.println("inside " + this.getClass().getName());
}
}
}
public void printSomething() {
System.out.println("inside " + this.getClass().getName());
}
}
Main.java
package com.oracle.javatests;
import com.oracle.javatests.ClassA.*;
// import com.oracle.javatests.ClassA.NestedAA.*; // Adding this will resolve NestedAB
public class Main {
public static void main (String[] args){
ClassA objA = new ClassA();
objA.printSomething();
NestedAA nestedAA = new NestedAA(); // Ok
NestedAB nestedAB = new NestedAB(); // Compiler error- NestedAB cannot be resolved to a type
}
}
The import statement does not import NestedAB type when using wildcards. A perhaps similar question led me to the java spec sheet which clarifies Type-Import-on-Demand Declarations :
A type-import-on-demand declaration allows all accessible types of a
named package or type to be imported as needed.
The accepted answer to the question implies that the on demand import declarations are not recursive. The reasoning is perhaps what Java considers "all accessible types of a named type", and the general concept of packages but I am falling short of connecting the dots and understand what accessible types means with respect to nested classes.
Can please anyone help explain how the type import and accessibility seem to work in java (while ignoring the arguable use of wildcard imports)
It's not heard to understand. import static com.foo.bar.*; is the exact same thing as import static com.foo.bar.[everything you can imagine here but without dots].
In other words, in your example, with import static pkg.ClassA.*; you can just write NestedAA without qualifiers and that works, because import static pkg.ClassA.NestedAA; would have made that work just the same.
You cannot write NestedAB unqualified and expect that to work; there is nothing you could possibly write instead of a * (which doesn't include dots) that would make that work, therefore, a star import doesn't make it work either.

How to static-import enums residing in separate files in Java?

Basically, here's the gist of my code:
// File: ShapeColor.java
package mypackage;
public enum ShapeColor
{
MAUVE,
PURPLE,
VIOLET;
}
// File: ShapeSize.java
package mypackage;
public enum ShapeSize
{
TINY,
SMALL,
MEDIUM,
LARGE,
HUGE,
COLOSSAL;
}
// File: Shape.java
import static mypackage.ShapeSize.*;
import static mypackage.ShapeColor.*;
public class Shape
{
private ShapeSize size;
private ShapeColor color;
public Shape()
{
this(ShapeSize.MEDIUM, ShapeColor.PURPLE);
}
public Shape(ShapeSize ss, ShapeColor sc)
{
this.size = ss;
this.color = sc;
}
}
How can I import static my enum definitions so I can access the enum values without qualifying them? I have all my .java source files slapped in one directory and I'm compiling from the command line. I have no idea about packages and how to "organize" my source files in directories (or if I even should do so). I've tried reading about these concepts but all that did was get me more confused. I learn much better through examples and demo code than by reading documentation, unfortunately.
EDIT:
After adding package mypackage; to the enum files and import static mypackage.* to the class file. I get the following errors:
.\ShapeSize.java:4: error: duplicate class: mypackage.ShapeSize
public enum ShapeSize
^
Shape.java:7: error: cannot access ShapeSize
private ShapeSize size;
^
bad source file: .\ShapeSize.java
file does not contain class ShapeSize
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Figured it out!
Moved both enum source files to sub-folder called mypackage and also normal-imported them in addition to import static their values. Shape class now starts like this:
import mypackage.ShapeSize;
import mypackage.ShapeColor;
import static mypackage.ShapeSize.*;
import static mypackage.ShapeColor.*;
EDIT:
Actually, no need to move the enum source files. Just had to standard-import them.
You can use:
import static mypackage.ShapeColor.PURPLE;
import static mypackage.ShapeSize.MEDIUM;

Java - Declare Class Once Use Anywhere

Very simple problem but im not understanding static correctly.
I have java file which holds my main and its call testMain.
With my testMain it makes many classes with use other classes.
E.g. testMain>>GUI and testMain>>model and testMain>>controller
Now i have a class called generatorTester which i would like to declare once like:
public static utils.generatorTester randomGen = new utils.generatorTester ();
(utils is my custom package for my common classes)
Why does the above line not aloud me to do the following
classNameOfMainFunction.randomGen
Im i programming wrong here? Is this even possbile.
I bassicly want to make the class globably and use it any where.
A public static field of a public class can be used anywhere, you just need to use the right syntax to access it.
If you declare:
package foo;
public class Global {
public static Some thing;
}
And do
import foo.Global;
you can access the field with
Global.thing
Alternatively, you can do
import static foo.Global.thing;
and access it with
thing
About the best you can get is this:
public abstract class GloballyUsed {
public static int method() { return 4;
/* determined by fair
* dice roll, guaranteed to be random */
}
and:
GloballyUsed.method();
to call elsewhere.
Note per comment (I just learned this) since Java 5 you can import just a specific method name as:
import static {package}.GloballyUsed.method;
Note I added the keyword abstract, this is to further convince you that you never actually instantiate GloballyUsed. It has no instances. You probably have some reading to do on what static means.

Importing packages in Java

How to import a method from a package into another program? I don't know how to import... I write a lil' code:
package Dan;
public class Vik
{
public void disp()
{
System.out.println("Heyya!");
}
}
and then, saved it in a folder named "Dan" and I compiled it. The .class file is generated. Then, I wrote this code below:
import Dan.Vik.disp;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
and I saved it outside the folder "Dan" and it says : "cannot find symbol"
I saved the first code in C:\Dan\Vik.java
and the second in C:\Kab.java
You don't import methods in Java, only types:
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
The exception is so-called "static imports", which let you import class (static) methods from other types.
In Java you can only import non-primitive types, or static methods/fields.
To import types use import full.package.name.of.TypeName;
//example
import java.util.List; //to import List interface
to import static methods/fields use
import static full.package.name.of.TypeName.staticMethod;
import static full.package.name.of.TypeName.staticField;
//example
import static java.lang.Math.max; //to import max method(s)
import static java.lang.Math.PI; //to import PI field
Take out the method name from in your import statement. e.g.
import Dan.Vik.disp;
becomes:
import Dan.Vik;
You should use
import Dan.Vik;
This makes the class visible and the its public methods available.
Here is the right way to do imports in Java.
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
You don't import methods in java. There is an advanced usage of static imports but basically you just import packages and classes.
If the function you are importing is a static function you can do a static import, but I don't think you are looking for static imports here.
In Java you can only import class Names, or static methods/fields.
To import class use
import full.package.name.of.SomeClass;
We can also import static methods/fields in Java and this is how to import
import static full.package.nameOfClass.staticMethod;
import static full.package.nameOfClass.staticField;
For the second class file, add "package Dan;" like the first one, so as to make sure they are in the same package; modify "import Dan.Vik.disp;" to be "import Dan.Vik;"

Java : The import collides with another import statement

I have imported an Existing Java Application into my Workspace .
I see that , a class with same name is present in different packages with in the Application.
For example a class named "Status.java" is present with in
com.tata.model.common.Status;
com.bayer.frontlayer.dao.Status;
When I tried to use both of them within a class, for example as shown below
import com.tata.model.common.Status;
import com.bayer.frontlayer.dao.Status;
public class Adapter
{
}
It started giving an error in Eclipse stating
The import com.bayer.frontlayer.dao.Status collides with another import statement
Is there anyway to solve this without changing the name of the classes??
Thank you.
You can use them explicitly without importing them, so the included package name differentiates between the two:
//No imports required!
public class Adapter
{
private com.tata.model.common.Status x;
private com.bayer.frontlayer.dao.Status y;
}
You can import just one of the classes and use the fully qualified name for the other one.
e.g.
import com.tata.model.common.Status;
//import com.bayer.frontlayer.dao.Status;
class SomeClass{
void someMethod(){
new Status(); // com.tata.model.common.Status
new com.bayer.frontlayer.dao.Status(); //com.bayer.frontlayer.dao.Status
}
}
Though I think it would be less confusing in your case if you just used the fully-qualified names for both classes.
Directly apply full Class Names wherever applicable. Eg-
public class SomeClass {
public someMethod() {
com.myapp.someotherpackage.Status = "something";
com.some.other.package.Status = "otherthing";
if(com.myapp.someotherpackage.Status == com.some.other.package.Status) {
}
....
}
}

Categories