Solved

basic java question

Posted on 2014-02-04
12
220 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
ID: 39833879
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
ID: 39833899
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
ID: 39833902
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
ID: 39833927
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
ID: 39833952
That's because you haven't put in the opening and closing braces for the methods.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39833956
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Ken Butters
ID: 39833963
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
ID: 39834929
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
ID: 39835898
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
ID: 39835905
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
ID: 39835979
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
ID: 39838127
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 80
advertisement module in core php 4 147
Java Jpanels and Jframe 8 28
Way to decrease size of apk file 9 46
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 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

19 Experts available now in Live!

Get 1:1 Help Now