Calling Parameterized Method from one class to another class
Hi, below is my sample code
test.java
public class test
int op1[][]=new int[][]{{1,2},{2,3},{4,5}};
int row=0;
public void test() {
int op2[][]=new int[][]{{2,3},{4,5},{6,7}};
row=1010;
getRow(row);
int[][] getList()
return op1;
public int getRow(int klm)
System.out.println("Row Value in GetRow Method is: "+row);
return klm;
test_2.java
package aa;
public class test_2
int a;
test t11=new test();
int abc[][] = t11.getList();
int rowCount=t11.getRow(a);
public void test_2()
System.out.println("Row Value is: "+rowCount);
System.out.println("Value in abc[1][1] is: "+abc[0][1]);
main_class.java
package aa;
public class main_class {
public static void main(String args[])
test ab=new test();
ab.test();
test_2 ba=new test_2();
ba.test_2();
public main_class() {
}now that my question is,for 'rowCount' in class test_2.java i need to get the value of 'row'(which is 1010)from class test.java. How could that be done?
Any reply is highly appreciable.
Thanks in advance
netbeans2eclipse wrote:
hmm, i did chaged the test object in main method to same as the test object that i used in test_2 (ie; i changed test ab=new test() in main_class.java to test t1=new test()).What? How does this help?
Again, simplify the problem
For instance, say you have a class, Fubar1, like so:
class Fubar1
int value = 0;
public void setValue(int v)
value = v;
public int getValue()
return value;
}and two different Fubar2 classes, both which try to get the value from Fubar1, one doing it badly:
class Fubar2Bad
Fubar1 f1 = new Fubar1(); // totally internal Fubar1 object
public int getFubar1Value()
return f1.getValue();
}and one that does it well, that holds a reference to whatever Fubar1 object is passed to it:
class Fubar2Good
Fubar1 f1;
public Fubar2Good(Fubar1 f1)
this.f1 = f1;
public int getFubar1Value()
return f1.getValue();
}Then if you test these:
public class TestFubars
public static void main(String[] args)
Fubar1 f1 = new Fubar1();
f1.setValue(35);
Fubar2Bad f2Bad = new Fubar2Bad();
Fubar2Good f2Good = new Fubar2Good(f1); // here we pass a reference to the main's Fubar1 object into the Fubar2Good object.
System.out.println("Bad: " + f2Bad.getFubar1Value());
System.out.println("Good: " + f2Good.getFubar1Value());
}
Similar Messages
-
How to call a method from one viewmodel to another viewmodel
Hi,
I have a functionality in first viewmodel , i need the functionality to be functioned in the second viewmodel . How can i do that . Help would be much appriciated .
Jeevan VinayPerhaps derive both of the classes from common base. The common functionality and data will be defined (moved) inside the base class.
-
Calling a method of one class from another withing the same package
hi,
i've some problem in calling a method of one class from another class within the same package.
for eg. if in Package mypack. i'm having 2 files, f1 and f2. i would like to call a method of f2 from f1(f1 is a servlet) . i donno exactly how to instantiate the object for f2. can anybody please help me in this regard.
Thank u in advance.
Regards,
FazliThis is what my exact problem.
i've created a bean (DataBean) to access the database. i'm having a servlet program (ShopBook). now to check some details over there in the database from the servlet i'm in need to use a method in the DataBean.
both ShopBook.java and DataBean.java lies in the package shoppack.
in ShopBook i tried to instantiate the object to DataBean as
DataBean db = new DataBean();
it shows the compiler error, unable to resolve symbol DataBean.
note:
first i compiled DataBean.java, it got compiled perfectly and the class file resides inside the shoppack.
when i'm trying to compile the ShopBook its telling this error.
hope i'm clear in explaining my problem. can u please help me?
thank u in advance.
regards,
Fazli -
Calling a drawLine() from one class to another from an ActionEvent
I have several JPanel objects called and placed on a JFrame. The JFrame has a RadioButton group with radio buttons. If I select a radio button and call a drawLine() method from a JPanel, I receive a "NullPointerException". Is it not possible to call this graphic method from one class to another?
Thanks for any input you can provide.
JohnRemember that each panel draws it's own current state. So you need the ActionPerformed to change the state in your target panel.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
public class PanelComm extends JPanel {
private SubPanelOne subPanelOne = new SubPanelOne();
private SubPanelTwo subPanelTwo = new SubPanelTwo();
public class SubPanelOne extends JPanel {
public SubPanelOne () {
setLayout ( new BorderLayout() );
setBorder ( BorderFactory.createTitledBorder ( "SubPanel One" ) );
Reactor reactor = new Reactor();
ButtonGroup group = new ButtonGroup();
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
JRadioButton buttonOne = new JRadioButton ( "One" );
buttonOne.addActionListener ( reactor );
group.add ( buttonOne );
radioPanel.add ( buttonOne );
JRadioButton buttonTwo = new JRadioButton ( "Two" );
buttonTwo.addActionListener ( reactor );
group.add ( buttonTwo );
radioPanel.add ( buttonTwo );
JRadioButton buttonThree = new JRadioButton ( "Three" );
buttonThree.addActionListener ( reactor );
group.add ( buttonThree );
radioPanel.add ( buttonThree );
add ( radioPanel ,BorderLayout.LINE_START );
protected void paintComponent ( Graphics _g ) {
super.paintComponent ( _g );
Graphics2D g = (Graphics2D)_g;
g.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
public class SubPanelTwo extends JPanel {
private JLabel text = new JLabel();
public SubPanelTwo () {
setLayout ( new BorderLayout() );
setBorder ( BorderFactory.createTitledBorder ( "SubPanel Two" ) );
text.setFont ( new Font ( "Verdana" ,Font.PLAIN ,30 ) );
text.setHorizontalAlignment ( JLabel.CENTER );
add ( text ,BorderLayout.CENTER );
protected void paintComponent ( Graphics _g ) {
super.paintComponent ( _g );
Graphics2D g = (Graphics2D)_g;
g.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
public void setChoice ( String cmd ) {
text.setText ( cmd );
public class Reactor implements ActionListener {
public void actionPerformed ( ActionEvent e ) {
subPanelTwo.setChoice ( e.getActionCommand() );
public PanelComm () {
setLayout ( new GridLayout ( 1 ,2 ) );
add ( subPanelOne );
add ( subPanelTwo );
// main
public static void main ( String[] args ) {
JFrame f = new JFrame ( "Panel Communication" );
f.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
f.getContentPane().add ( new PanelComm() ,BorderLayout.CENTER );
f.setSize ( 320 ,120 );
f.setVisible ( true );
} // main
} -
Moving a method from one class to another issues
Hi, im new. Let me explain what i am trying to achieve. I am basically trying to move a method from one class to another in order to refactor the code. However every time i do, the program stops working and i am struggling. I have literally tried 30 times these last two days. Can some one help please? If shown once i should be ok, i pick up quickly.
Help would seriously be appreciated.
Class trying to move from, given that this is an extraction:
class GACanvas extends Panel implements ActionListener, Runnable {
private WorldMenuItems speedMenuItems, designMenuItemsPlantGrowth, designMenuItemsPlantCount;
private WorldMenuItems designMenuItemsPlantEaten, designMenuItemsApproximatePopulation;
private WorldMenuItems designMenuItemsEatersBorn,designMenuItemsMutationProbability;
private WorldMenuItems designMenuItemsCrossoverProbability;
MenuBar getMenuBar() {
menuBar = new MenuBar();
addControlItemsToMenuBar();
addSpeedItemsToMenuBar();
addWorldDesignItemsToMenuBar();
return menuBar;
This is the method i am trying to move (below)
public void itemsInsideWorldDesignMenu() {
designMenuItemsPlantGrowth = new WorldMenuItems("Plants Grow",
new String[] { "In Rows", "In Clumps", "At Random",
"Along the Bottom", "Along the Edges" }, 1);
designMenuItemsPlantCount = new WorldMenuItems("Number Of Plants",
new String[] { "50", "100", "150", "250", "500" }, 3);
designMenuItemsPlantEaten = new WorldMenuItems("When a Plant is Eaten",
new String[] { "It grows back somewhere",
"It grows back nearby", "It's Gone" }, 0);
designMenuItemsApproximatePopulation = new WorldMenuItems(
"Approximate Population", new String[] { "10", "20", "25",
"30", "40", "50", "75", "100" }, 2);
designMenuItemsEatersBorn = new WorldMenuItems("Eaters are Born",
new String[] { "At the Center", "In a Corner",
"At Random Location", "At Parent's Location" }, 2);
designMenuItemsMutationProbability = new WorldMenuItems(
"Mutation Probability", new String[] { "Zero", "0.25%", "0.5%",
"1%", "2%", "3%", "5%", "10%" }, 3);
designMenuItemsCrossoverProbability = new WorldMenuItems(
"Crossover Probability", new String[] { "Zero", "10%", "25%",
"50%", "75%", "100%" }, 4);
Class Trying to move to:
class WorldMenuItems extends Menu implements ItemListener {
private CheckboxMenuItem[] items;
private int selectedIndex = -1;
WorldMenuItems(String menuName, String[] itemNames) {
this(menuName, itemNames, -1);
WorldMenuItems(String menuName, String[] itemNames, int selected) {
super(menuName);
items = new CheckboxMenuItem[itemNames.length];
for (int i = 0; i < itemNames.length; i++) {
items[i] = new CheckboxMenuItem(itemNames);
add(items[i]);
items[i].addItemListener(this);
selectedIndex = selected;
if (selectedIndex < 0 || selectedIndex >= items.length)
selectedIndex = 1;
items[selectedIndex].setState(true);
public int getSelectedIndex() {
return selectedIndex;
public void itemStateChanged(ItemEvent evt) { // This works on other systems
CheckboxMenuItem newSelection = (CheckboxMenuItem)evt.getSource();
for (int i = 0; i < items.length; i++) {
if (newSelection == items[i]) {
items[selectedIndex].setState(false);
selectedIndex = i;
newSelection.setState(true);
return;Ok i've done this. I am getting an error on the line specified. Can someone help me out and tell me what i need to do?
GACanvas
//IM GETTING AN ERROR ON THIS LINE UNDER NAME, SAYING IT IS NOT VISIBLE
WorldMenuItems worldmenuitems = new WorldMenuItems(name, null);
public MenuBar getMenuBar() {
menuBar = new MenuBar();
addControlItemsToMenuBar();
addSpeedItemsToMenuBar();
worldmenuitems.addWorldDesignItemsToMenuBar();
return menuBar;
class WorldMenuItems extends Menu implements ItemListener {
private WorldMenuItems speedMenuItems, designMenuItemsPlantGrowth, designMenuItemsPlantCount;
private WorldMenuItems designMenuItemsPlantEaten, designMenuItemsApproximatePopulation;
private WorldMenuItems designMenuItemsEatersBorn,designMenuItemsMutationProbability;
private WorldMenuItems designMenuItemsCrossoverProbability;
GACanvas gacanvas = new GACanvas(null);
private CheckboxMenuItem[] items;
private int selectedIndex = -1;
WorldMenuItems(String menuName, String[] itemNames) {
this(menuName, itemNames, -1);
WorldMenuItems(String menuName, String[] itemNames, int selected) {
super(menuName);
items = new CheckboxMenuItem[itemNames.length];
for (int i = 0; i < itemNames.length; i++) {
items[i] = new CheckboxMenuItem(itemNames);
add(items[i]);
items[i].addItemListener(this);
selectedIndex = selected;
if (selectedIndex < 0 || selectedIndex >= items.length)
selectedIndex = 1;
items[selectedIndex].setState(true);
public int getSelectedIndex() {
return selectedIndex;
public void itemStateChanged(ItemEvent evt) { // This works on other systems
CheckboxMenuItem newSelection = (CheckboxMenuItem)evt.getSource();
for (int i = 0; i < items.length; i++) {
if (newSelection == items[i]) {
items[selectedIndex].setState(false);
selectedIndex = i;
newSelection.setState(true);
return;
public void itemsInsideWorldDesignMenu() {
designMenuItemsPlantGrowth = new WorldMenuItems("Plants Grow",
new String[] { "In Rows", "In Clumps", "At Random",
"Along the Bottom", "Along the Edges" }, 1);
designMenuItemsPlantCount = new WorldMenuItems("Number Of Plants",
new String[] { "50", "100", "150", "250", "500" }, 3);
designMenuItemsPlantEaten = new WorldMenuItems("When a Plant is Eaten",
new String[] { "It grows back somewhere",
"It grows back nearby", "It's Gone" }, 0);
designMenuItemsApproximatePopulation = new WorldMenuItems(
"Approximate Population", new String[] { "10", "20", "25",
"30", "40", "50", "75", "100" }, 2);
designMenuItemsEatersBorn = new WorldMenuItems("Eaters are Born",
new String[] { "At the Center", "In a Corner",
"At Random Location", "At Parent's Location" }, 2);
designMenuItemsMutationProbability = new WorldMenuItems(
"Mutation Probability", new String[] { "Zero", "0.25%", "0.5%",
"1%", "2%", "3%", "5%", "10%" }, 3);
designMenuItemsCrossoverProbability = new WorldMenuItems(
"Crossover Probability", new String[] { "Zero", "10%", "25%",
"50%", "75%", "100%" }, 4);
public void addWorldDesignItemsToMenuBar() {
gacanvas = new GACanvas(null);
itemsInsideWorldDesignMenu();
Menu designMenuItems = new Menu("WorldDesign");
designMenuItems.add(designMenuItemsPlantGrowth);
designMenuItems.add(designMenuItemsPlantCount);
designMenuItems.add(designMenuItemsPlantEaten);
designMenuItems.add(designMenuItemsApproximatePopulation);
designMenuItems.add(designMenuItemsEatersBorn);
designMenuItems.add(designMenuItemsMutationProbability);
designMenuItems.add(designMenuItemsCrossoverProbability);
gacanvas.menuBar.add(designMenuItems); -
How to call main method in one class from another class
Suppose i have a code like this
class Demo
public static void main(String args[])
System.out.println("We are in First class");
class Demo1
public static void main(String args[])
System.out.println("We are in second class");
In the above program how to call main method in demo calss from Demo1 class......???????No, i dont know how to call other than main methods from other classes
And one more doubt i have i.e. Like in C can we see the execution of the program like how the code is being compiled step by step and how the value of the variable changes from step to step like(Add Watch)......... -
What are the methods called while navigating from one applet to another one
Hi All,
Could any one brief me about "When you navigate from one applet to another what are the methods called ?".
Thanks in advance.
Best Regards,
N.Madhusudhanan.http://forum.java.sun.com/thread.jsp?forum=421&thread=426771&tstart=0&trange=100
-
How to call a method from a class without creating an instance fromthisclas
I want to create a class with methods but I want to call methods from this class without creating an instance from this class. Is this possible?
Is there in Java something like class methods and object methods????keyword 'static' my friend.
public class foo {
public static void bar {
System.out.println ("hello");
foo.bar() -
Calling a method from abstarct class
Hi Experts,
Am working on ABAP Objects.
I have created an Abstract class - with method m1.
I have implemented m1.
As we can not instantiate an abstract class, i tried to call the method m1 directly with class name.
But it it giving error.
Please find the code below.
CLASS c1 DEFINITION ABSTRACT.
PUBLIC SECTION.
DATA: v1 TYPE i.
METHODS: m1.
ENDCLASS. "c1 DEFINITION
CLASS c1 IMPLEMENTATION.
METHOD m1.
WRITE: 'You called method m1 in class c1'.
ENDMETHOD. "m1
ENDCLASS. "c1 IMPLEMENTATION
CALL METHOD c1=>m1.
Please tell me what is wrong and how to solve this problem.
Thanks in Advance.Micky is right, abstract means not to be instantiated. It is just a "template" which you can use for all subsequent classes. I.e you have general abstract class vehicle . For all vehicles you will have the same attributes like speed , engine type , strearing , gears etc and methods like start , move etc.
In all subsequent classes (which inherit from vehicle) you will have more specific attributes for each. But all of these classes have some common things (like the ones mentioned above), so they use abstract class to define these things for all of them.
Moreover there is no sense in creating instance (real object) of class vehicle . What kind of physical object would vehicle be? there is no such object in real world, right? For this we need to be more precise, so we create classes which use this "plan" for real vehicles. So the abstract class here is only to have this common properties and behaviour defined in one place for all objects which will have these. Abstract object however cannot be created per se. You can only create objects which are lower in hierarchy (which are specific like car , ship, bike etc).
Hope this claryfies what we need abstract classes for.
Regards
Marcin -
Calling swc method from .as class
so i've been given a .swc and i've been told that its got a
method on it called doSomething();
for one reason or another i can't include the swf in my fla.
what i need to do is somehow include the swc in an AS3 class and
then call the swc method therefrom.
so i got it here, in a directory called 'swcs' its called
'someSWC.swc' and i need to import it to someClass.as and call
doSomething() on it.
and i have no idea of how i'd do that..........
anyone?hi andrei1
sure, ive had to bash my head against this for a few days
come up with a solution. yes i found out that if you can bust open
a swc as you suggest you find an xml file which indexes the swc
contents and a swf that contains the various
methods/assets/graphics/etc. when you look through the tags in the
indexical xml you can find references to AS3 objects like this:
<script name="com/website/package/subpackage/SomeClass"
mod="1196781480890" >
etc. so then, once i knew the package structure and method
etc. names i experimented with putting the swc that in a folder
called 'swc' which was inside my source folder 'src', setting my
AS3 classpaths to:
./src
./swc
and subsequently including the swc objects in my AS3 classes
like this:
package com.mystuff.myapp {
import com.website.package.subpackage.*;
using fdt to edit .as files and compile swfs it constantly
complains about the missing classes at
'com.website.package.subpackage.*' but compiles the swf ok so i'm
assuming therefore that fdt lacks the ability to look inside swcs
for source classes. could be wrong i guess, might just need to
tweak an fdt setting to ensure that src/swc is included in the
classpath or that swcs are included as class source or something.
if anyone knows if thats true or not be good if you could leave a
comment after this one.
otherwise that's how i fixed it for myself, hope this answer
can help you too. :) -
Calling a method from custom Class in WD Application
Hi,
I wrote a piece of code in method SEARCH for my WD Application.
Instead of diectly callling the method I would like to create a class and then call the method.
How do we proceed?
Do we use SE24 first or can we start creating class from WD application itself?
Rgds
varaHi Vara,
Either way it is possible if you are creating from web dynpro then you can create assistance class from component properties there you can write one method and to access that method always you have an instance wd_assist.
From se24 create a class and declare a method. if you declare level of method is instance then you have to create object first in application and then access your method using created instance. -
How to call a form from one form to another form with parameter..
i'm having a from that will shows the info about the orders that the client has. and i want to show the information of the client. for that i have created one form and i want to call the form from my current form with the given client name as a parameter. then the client_info form has to query with the given parameter. so im passing the :client_order_info.client_name as parameter to the call_form('client_info.fmx',:client_order_info.client_name); but my question is how to access that parameter in the client_info form.. plz help me.. i tried a lot ..
Create a parameter (object navigator -> parameters) with the name client_name and you can access that value in your client info form using :PARAMETER.client_name.
P.S. while calling you can pass parameter using 'cilent_name =>' to make sure that the correct parameter is getting assigned to the value. -
How to call a method of one dc in another dc
Hi,
i have two dcs dc1 and dc2
i want to call the method of dc1 interface controller in component controller of dc2
plz help me, how to acheive it
Thanks & regards,
v santhoshHi Vanama,
Hope you have defined a method say method1() in interface controller of DC1
Add this to public part.
In DC2 , set DC1 as used DC.
give <DCUsageName>
now you can access the method in DC1 like this.
wdThis.wdGet<DCUsageName>().getmethod1();
Regards,
Murtuza -
Calling a method from different class
Hey peeps,
say i have a method in another class and i wish to call it, what would the code be for that?
say for example the method was called createNewShoeBox and i was in another class?
hope that makes sencejermaindefoe wrote:
lol! id love to have one, and in which case would love to have the money for one as id definatley take it up,In the following part, you're saying you're a student. So there are peers you can ask, if not your professor. There are certainly also some who actualy know some programming and would teach you if you just asked them.
im a first year student bear in mind though lol!!! and ma struggling, I see that, and the way you handle your problems, that won't change for the next few years.
i may not be as good at java as you, but i can do other things better than you can, and thats life, we all have to start somewhereWhat you also can't do as well as I do is "realizing that using an internet forum is one of the worse ways to have people helping you to learn programming". You're lacking a lot of understanding for concepts where the label "basic" is almost exaggerating. I'm sure you can get that understanding, but being spoon-fed through a forum with no visible effort to figure things out is not the way to do it. -
How to call a function from one viewmodel to another viewmodel
Hi ,
How to call a funtion of another viewmodel in a viewmodel . This help would be much appreciated
Thank You
Jeevan VinayPlease close your previous threads by marking helpful posts as answer before you start a new one.
You need to either get a reference to the second view model from the first one or you could use some kind of messenger or event aggregator to pass a message from the first view model to the second one. The latter approach is preferable as it doesn't create
a tight coupling between the two view model classes.
Please refer to my blog post for more information and an example of how to use Prism's EventAggregator to do this:
http://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/
You will need to download and install Prism to be able use the EventAggregator class:
https://www.nuget.org/packages/Prism/
Another popular framework out there is the MvvmLight which contains a Messenger class that works pretty much the same. Andy has provided and example of how to use this one:
http://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx
You can download and install MvvmLight via Nuget (Tools->Nuget Package Manager->Package Manager Console in Visual Studio):
https://www.nuget.org/packages/MvvmLight/
Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question.
Maybe you are looking for
-
Hello, I'm a IT Consultant. One of our users has lost their calendar entries from 2013 August and older. We do have a recovery solution to get this data back, but to prevent this from ever happening again. Is there a option to do so? What we so far f
-
Error while running the vsm application
Hi, I am trying to deploy the virtual shopping mall application. But during the server start up the below error is thrown. Warning: Error creating table: Io exception: The Network Adapter could not establish the connection. I searched the forum for t
-
hi all when i open form from another one the connection string that contain user name & password appears in url this in applicaion server10g
-
Drop down lists in Interactive forms:HCM Forms&Processes
Hi Experts, I am working in HCM Forms and Process. I have 2 dropdown lists in my interactive form such as COUNTRY, STATE. The STATE dropdown should be populated based on the COUNTRY field dynamically. I have written the logic for STATE dropdown in t
-
Hai, I have a table UI element populated with values. I have to download the contents thro excel file. I dont want to do in alv. pls give some suggestions