6 Replies - 18181 Views - Last Post: 30 January 2009 - 02:23 PM Rate Topic: -----

#1 willm29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 09

Passing a class object in function parameters ?

Posted 21 January 2009 - 06:24 AM

Hi everyone. Just surfed in here to dream in code this afternoon whilst looking for an answer to a Java question that I have been spending hours on this morning. I hope you might be able to help me!! I am getting very frustrated by what is I think a very simple error.

I am trying to create a java class object and pass it to a method as a parameter.... sounds simple!! but I am getting the following error:

myclass.java:22: cannot find symbol
symbol  : constructor mysubclass(java.lang.String)
location: class myclass.mysubclass
		r1 = method1(new mysubclass("23"));


(with the arrow pointing to the n of 'new' in the last line)

The code is the following:
(I have shortened/edited it for testing)

public class myclass 
{
	class mysubclass
	{
		int i;
		
		public void main(String v)
		{
		  i = Integer.parseInt(v);
		}
		
		public int doConvert()
		{
		  return i;
		}
	}
	
	public void main(String[] args) 
	{
		int r1, r2;
		
		r1 = method1(new mysubclass("23"));
		
		r2 = method2(new String("23"));
	}
	
	public int method1(mysubclass t)
	{
	  return t.doConvert();
	}
	
	public int method2(String t)
	{
	  return 32;
	}
	
}




What the hell is going on? I have spent the whole morning trying to figure this out and it is really beginning to annoy me!! :v:

Thanks for your help

This post has been edited by willm29: 21 January 2009 - 06:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing a class object in function parameters ?

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Passing a class object in function parameters ?

Posted 21 January 2009 - 07:24 AM

It's wrong right here..

public class myclass 
{
	class mysubclass
	{



Java expects a constructor, not a class definition.
I think you need to go back to the basics coz the code is filled with errors.

This post has been edited by Gloin: 21 January 2009 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Passing a class object in function parameters ?

Posted 21 January 2009 - 07:31 AM

* different classes belong in different files
* main method has to be declared static
* methods must either be invoked on some object or be declared static

If you have problem understanding either of these 3 comments I suggest you take another look in your java-book (start from the first page). Also take a look at some working code, compare to yours to see where you've done wrong.
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Passing a class object in function parameters ?

Posted 21 January 2009 - 08:06 AM

What you are effectively doing is creating a nested class, and then you call the nested class constructor with a string argument, and there is no constructor of this type for that nested class.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Passing a class object in function parameters ?

Posted 21 January 2009 - 05:37 PM

View PostGloin, on 21 Jan, 2009 - 06:24 AM, said:

It's wrong right here..

public class myclass 
{
	class mysubclass
	{



Java expects a constructor, not a class definition.
I think you need to go back to the basics coz the code is filled with errors.

Sorry Gloin perfectly legal to have an inner class and it can be declare anywhere
myclass will inherit from Object constructor with no paramter without problem

however:
- mysubclass does not have a constructor that receives a String as parameter
- at least the main of myclass should be declared static
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Passing a class object in function parameters ?

Posted 21 January 2009 - 06:02 PM

I never used nested classes like that except for once I think. However, considering the code posted I assumed the nesting was not intentional.
Was This Post Helpful? 0
  • +
  • -

#7 willm29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 09

Re: Passing a class object in function parameters ?

Posted 30 January 2009 - 02:23 PM

View Postpbl, on 21 Jan, 2009 - 04:37 PM, said:

View PostGloin, on 21 Jan, 2009 - 06:24 AM, said:

It's wrong right here..

public class myclass 
{
	class mysubclass
	{



Java expects a constructor, not a class definition.
I think you need to go back to the basics coz the code is filled with errors.

Sorry Gloin perfectly legal to have an inner class and it can be declare anywhere
myclass will inherit from Object constructor with no paramter without problem

however:
- mysubclass does not have a constructor that receives a String as parameter
- at least the main of myclass should be declared static



Yes that was the reason..... It took me a while to work it out in the end but I got there.

Apologies for late reply, thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1