Method calling

Method calling without having to create multiple copies of the same cl

Page 1 of 1

13 Replies - 1270 Views - Last Post: 17 January 2010 - 02:36 PM Rate Topic: -----

#1 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Method calling

Posted 17 January 2010 - 07:25 AM

Hi there, I was just wondering if it was possible to call a method from another class without having to create several copies of it??? I have used the following
	public DSDClientGUI() 
	{
		initComponents();
		try
		{
		Client client2 = new Client();

		
		}
		 catch(IOException e)
		{
			System.out.println("received and IO Exception + e");
		}
	 }  


I want to be able to now use client2 in another method but i can't seem to get it to work could anyone help me

Regards,

Gda2004

Is This A Good Question/Topic? 0
  • +

Replies To: Method calling

#2 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Method calling

Posted 17 January 2010 - 07:42 AM

You won't be able to access it outside of the constructor because it is lexically scoped to that method. You could always make a class variable and then access it in that way.
Was This Post Helpful? 0
  • +
  • -

#3 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Method calling

Posted 17 January 2010 - 07:47 AM

View PostDark_Nexus, on 17 Jan, 2010 - 06:42 AM, said:

You won't be able to access it outside of the constructor because it is lexically scoped to that method. You could always make a class variable and then access it in that way.


Thanks for your reply I was just wondering how exactly do you make a class variable??

Regards,

Gda2004
Was This Post Helpful? 0
  • +
  • -

#4 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Method calling

Posted 17 January 2010 - 07:52 AM

Here is an example of an instance variable in java:

class MyClass {
	private int my_instance_variable;

	int getInstanceVariable(void) {
		return my_instance_variable;
	}

	void setInstanceVariable(int nval) {
		my_instance_variable = nval;
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Method calling

Posted 17 January 2010 - 07:58 AM

thanks for your reply. I simply want to use one of client2's methods i type client2. and i get red underneath it. I want to call it in another method other than the one with the client client2 = new client() bit

Regards,

Gda2004
Was This Post Helpful? 0
  • +
  • -

#6 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Method calling

Posted 17 January 2010 - 07:59 AM

As stated above, you will have to make it an instance variable to use it outside of your constructor method. You'll notice in the example code I provided I am accessing the same variable in two different methods which is precisely what you are describing. Try to mimic the example I gave.

This post has been edited by Dark_Nexus: 17 January 2010 - 08:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Method calling

Posted 17 January 2010 - 08:00 AM

i mean when i type client2. the methods do not come up when i type in the full thing client2.copy() which is a method i want to use i still get errors because it does not know about client2 because i am calling the client2.copy() in another method other than the one with the new client() in

Regards

gda2004
Was This Post Helpful? 0
  • +
  • -

#8 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Method calling

Posted 17 January 2010 - 08:02 AM

Right, for the third time, you need to make client2 and instance variable inside the DSDClientGUI class.
Was This Post Helpful? 0
  • +
  • -

#9 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Method calling

Posted 17 January 2010 - 08:16 AM

View PostDark_Nexus, on 17 Jan, 2010 - 07:02 AM, said:

Right, for the third time, you need to make client2 and instance variable inside the DSDClientGUI class.


Right i think i get you. I however thought that once you had created an instance of class if it was a public class that any other class would be able to call the methods.

if DSDGUI created a client
then DSDGUI would be able to use the methods within the Client class as if they were their own obv

Regards,

gda3004
Was This Post Helpful? 0
  • +
  • -

#10 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Method calling

Posted 17 January 2010 - 08:18 AM

I am not talking about instantiating the class. I am talking about making a variable part of a class so that all methods can access it.
Was This Post Helpful? 0
  • +
  • -

#11 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Method calling

Posted 17 January 2010 - 08:22 AM

I am talking about accessing a method in the client class from DSDGUI class. i wanted to know how to call the method from another class
Was This Post Helpful? 0
  • +
  • -

#12 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Method calling

Posted 17 January 2010 - 08:24 AM

Something like

class DSDClientGUI {

	private Client client2; /* Here's your instance variable */

	public DSDClientGUI() 
		{
			initComponents();
			try
			{
				client2 = new Client();
			}
			 catch(IOException e)
			{
				System.out.println("received and IO Exception + e");
			}
	 } 

	 public void SomeOtherMethod(void) {
		 client2.copy();
	 }
}


Was This Post Helpful? 0
  • +
  • -

#13 gda2004   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Method calling

Posted 17 January 2010 - 08:28 AM

View PostDark_Nexus, on 17 Jan, 2010 - 07:24 AM, said:

Something like

class DSDClientGUI {

	private Client client2; /* Here's your instance variable */

	public DSDClientGUI() 
		{
			initComponents();
			try
			{
				client2 = new Client();
			}
			 catch(IOException e)
			{
				System.out.println("received and IO Exception + e");
			}
	 } 

	 public void SomeOtherMethod(void) {
		 client2.copy();
	 }
}



That was what i was after thanks for your help
Regards,

Gda2004
Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Method calling

Posted 17 January 2010 - 02:36 PM

Ouf....
   Client client2;
   public DSDClientGUI() 
	{
		initComponents();
		try
		{
			 client2 = new Client();
		}
		 catch(IOException e)
		{
			System.out.println("received and IO Exception + e");
		}
	 }  


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1