Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

how to access fucntion in other java file but in the same package

hi, again :)

says,

i have one package called "pacMAN"

and 2 files in pacMAN,  boy.java and girl.java

how can i access some function in boy while i writting code in girl

thx
Tom
0
tom_mk
Asked:
tom_mk
  • 9
  • 7
  • 6
  • +2
2 Solutions
 
girionisCommented:
Do

boy b = new boy();
boy.function();

Ps. it is always a godo practice to name classes with the first letter capital , e.g. Boy
0
 
petmagdyCommented:
in girl:

boy theBoy = new boy();
//call any methid
theBoy.doBoyAction();
0
 
zzynxSoftware engineerCommented:
You can by default.

if each girl.java and boy.java file contain as first line

package pacMAN;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
girionisCommented:
As a side note, if both files are in the same package you do not need to import them. If they are in another package and you want to use them you will have to import them. So lets say that your girl class was in the package pacMANGirl you would have to do in your girl class:

import pacMAN.boy;
...
0
 
petmagdyCommented:
also gril can have a method like this:

class girl
{
 public static brushMyHair()
{
//bla bla
}
}

in this case from boy u will not need to instantiate girl class just:

girl.brushMyHair();
0
 
girionisCommented:
> boy b = new boy();

I mean of cource b.function();
0
 
girionisCommented:
Damn it...

> boy b = new boy();
> boy.function();

boy b = new boy();
b.function();
0
 
petmagdyCommented:
sorry miss type the brushMyHair() method signature will look like this:

 public static void brushMyHair()
{
//bla bla
}
0
 
tom_mkAuthor Commented:
i have

package pacMAN;

on the first line of girl.java and boy.java

but i can't use the function of boy.java directly

0
 
tom_mkAuthor Commented:
says .. in girl i have makeTea();

how can i call makeTea() without

girl tea = new girl();
tea.makeTea();


i just need to call makeTea() directly  ,, can I?

Tom
0
 
petmagdyCommented:
if u want to call makeTea() make it static as in my comment
0
 
RuadRauFlessaCommented:
Yeah like petmagdy said you could make your method static like he posted

class Girl {
>public static void brushMyHair()
>{
>//bla bla
>}
}

and then call it as follows:

Girl.brushMyHair();
0
 
petmagdyCommented:
like this:
class girl
{
 public static makeTea()
{
//bla bla
}
}

in this case from boy u will not need to instantiate girl class just:

girl.makeTea();

0
 
tom_mkAuthor Commented:
thx

Tom
0
 
girionisCommented:
> says .. in girl i have makeTea();

> how can i call makeTea() without

Just do:

makeTea();

You can call girl functions from within girl by just using their name.
0
 
girionisCommented:
> i just need to call makeTea() directly  ,, can I?

Yes, look at my comment above.
0
 
zzynxSoftware engineerCommented:
> says .. in girl i have makeTea();

> how can i call makeTea() without

In the code of the girl class you can call it directly yes.
In the code of another class you need

1)         girl.makeTea()
    if makeTea() is a static function

2)  girl g = new girl();
     g.makeTea()

     if it is no static function
0
 
tom_mkAuthor Commented:
it still not working by some reason

this is my func.java

i tring

func.addButton("OK",panel);

in other java file which is also inside package myclient,

however, i still receive this error

addButton( .......... ) has private access in myclient.func

i don't know y... so i paster the code below..



package myclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class func
{
      private static void addButton(String text, JPanel container)
      {
       
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 10));
       
        JButton but = new JButton(text);
               
        panel.add(but);
       
        container.add(panel);
               
    }
      
      
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

package myclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;


public class BidPanel extends JPanel
{
      
      private JPanel bidP;
      private String itemName = "car";
      
      
      public BidPanel()
      {
            bidP = new JPanel();
            bidP.setLayout(new BoxLayout(bidP, BoxLayout.PAGE_AXIS));
            
            addLabel("You have selected item : " + itemName, bidP);
            addLabelWithField("Bid Price $",bidP);
            
            func.addButton("Confirm Bid",bidP);
            
            add(bidP);
      }
      
      
      private static void addLabel(String text, JPanel container)
      {
       
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 10));
       
        JLabel comment = new JLabel(text);
       
        panel.add(comment);
       
        container.add(panel);
               
    }
   
      private static void addLabelWithField(String text, JPanel container)
      {
       
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 10));
       
        JLabel comment = new JLabel(text);
        JTextField textfield = new JTextField(10);
       
        panel.add(comment);
        panel.add(textfield);
       
        container.add(panel);
               
    }
      
      
}
0
 
zzynxSoftware engineerCommented:
change to:
public static void addButton(String text, JPanel container)
0
 
zzynxSoftware engineerCommented:
private ==> public
0
 
zzynxSoftware engineerCommented:
Read this: Controlling Access to Members of a Class http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
0
 
zzynxSoftware engineerCommented:
A private function is accessible only to the class in which it is defined
0
 
zzynxSoftware engineerCommented:
changing private => protected would also work, since both classes are in the same package
0
 
tom_mkAuthor Commented:
thx xxynx.. it fianally works
0
 
girionisCommented:
:)
0
 
zzynxSoftware engineerCommented:
>> thx xxynx.. it fianally works
Again, can you please comment on the fact the you gave us a B-grade?
0
 
petmagdyCommented:
wasn't my  comments regarding static method useful :-( ?
but any way, no problem
0
 
zzynxSoftware engineerCommented:
You can't win them all petmagdy :°)
(I guess that was just considered as extra info the author didn't ask for)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 9
  • 7
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now