Solved

basic java question

Posted on 2014-02-04
12
219 Views
Last Modified: 2014-02-18
I how one spaghetti java class. Decided to divide it into smaller classes.

created a directory xxxx where I moved the original java code. I added package 'xxxx' on the top.

package xxxx
class A {

 main()
{

}

}

Then moved couple of static method to a new class without main :

package xxxx
class B {
 
 public static method A();

public static method B();
}

When I am trying to call a method in class A from class B :  B.A()
I am getting an compiler error message that B is not found
0
Comment
Question by:sniger
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
Sounds like you did not create a new instance of class B within Class A.

somewhere within class A... you need to do something like

B newInstanceOfB = new B();

once you have the new instance... then you can refer to the static method.

newInstanceOfB.A();
0
 

Author Comment

by:sniger
Comment Utility
when I did:
  Styles  style = new Styles()  ( Styles is a class B)


  symbol  : class Styles                              
  location: class xxxx.Excel                        
                        Styles  style = new Styles()

I got a message  cannot find symbol. Class styles is in xxxx directory
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
Ken,

It's a static method; you don't need an instance of the class.

sniger,

If the classes are in two different packages, class B needs to be declared public in order for class A to see it.
0
 

Author Comment

by:sniger
Comment Utility
the classes are in the same packages. I declared class B as public
and call static method B.A() and  I am still getting the same error
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
That's because you haven't put in the opening and closing braces for the methods.
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
sniger,

Can you post more of your actual code? It's tough to tell what's going on from the pseudocode you've posted.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
CPColin is right...thanks for the correction... I was thinking that we had to declare an instance of the class because the class wasn't static... but since we are calling a static method... you are right...we should be able to invoke directly using the class name.

@sniger... I created a test program with your conditions... and had no issue calling B.A();
Did you put the code for Class A in A.java and the code for class B in B.java?

In A.java I had this code:
package test;

public class A {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		B.method1();
	}
}

Open in new window

in B.java I had this code...
package test;
class B {
	static void method1()
	{ System.out.println("method1");}
	
	static void method2()
	{ System.out.println("method2");}
}

Open in new window

result of "method1" prints to console.
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
Comment Utility
Assuming you have installed JDK correctly & you are able to compile the class in appropriate directory.
Are you using eclipse IDE? If yes, then it will show you error in eclipse itself even before running your class.
Please check the .class file for both the java classes.
0
 

Author Comment

by:sniger
Comment Utility
Ken:

I copied your examples and got the same error message. I created directory test
and put those 2 sources when I am trying to compile, I am getting error message:


A.java:7: cannot find symbol  
symbol  : variable B          
location: class test.A        
                B.method1();  
                     
1 error
0
 

Author Comment

by:sniger
Comment Utility
Ken:

I am using command line...my classpath is as follows:

echo $CLASSPATH                                                              
.:/java/jt400.jar:/java/poi-3.9.jar:/java/poi-ooxml-3.9.jar:/java/poi-ooxml-s
chemas-3.9.jar:/java/xmlbeans-2.3.0.jar:/java/dom4j-1.6.1.jar:/java/javax.mai
l.jar                                                                        
$
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
Comment Utility
when you compile them... you need to do the following:

>javac A.java B.java

I get the error you got when I just compiled A.java by itself.
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
Comment Utility
javac A.java should work as Class B has been invoked from class A.

Ken, Why do you think javac A.java B.java  command needs to be executed?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now