Java Questions

JC0133

Senior member
Nov 2, 2010
201
1
76
Is there another way to pass information to a class from another class besides a Method or a Constructor?

My professor in my Java Class told us not to pass any values to the constructor (leaving it as a default Constructor) and not to create any public methods in the class. So If I have data in one class and I need to pass it to another class what is the best method to do this?

I thought about setting up a private method with a parameter list but if it is private doesn't that mean it is only usable in that same class??
 

ioni

Senior member
Aug 3, 2009
619
11
81
Are you sure he didn't say to not make the member variables public instead of the methods? No public methods in a class doesn't make any sense.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Yes, private methods are accessible only to members of the class.

If you have a parameterless constructor, no public methods, and no public fields, then the only way you could pass data to an instance of the class is drop it somewhere for pickup.

Is there another way? Sure there is. Have it listen on a socket :). Or open a named pipe or memory-mapped file. Maybe just drop the data into a file that the instance looks for.

Of course, this is dumb.
 

JC0133

Senior member
Nov 2, 2010
201
1
76
No looks like there was some confusion with the assignment. Basically I will have 3 total classes(including the class that has the main function in it). one class I will pass parameters threw the constructor and another class I can have a public method to take in that class value.

I am getting an error tho cause I need to pass this value inside of a nested for loop.
I am not sure if there is a special why to show my code here or if I should put the entire thing but I will put the key point of of the error I am getting. let me know if I am doing this wrong. First time ever doing this.

Code:
//this is inside of the main function
deckHand full; //object for full deck class.
deckHand empty; //object for empty deck class.
card type; //object for card class.

for (newSuit=1; newSuit <= 4; newSuit++) {
    for (newValue=1; newValue <= 13; newValue++) {
        type=new card(newSuit,newValue);

        System.out.println(type);  //working
    }
}
System.out.println(type);  //getting an error

Code:
 tags for the win - Markbnj[/b]
 
Last edited by a moderator:

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
....That's odd. Java isn't a main language for me, but I can't see anything that would cause that to not work.

What exactly is the error it's giving you?
 

beginner99

Diamond Member
Jun 2, 2009
5,315
1,760
136
Yes, private methods are accessible only to members of the class.

If you have a parameterless constructor, no public methods, and no public fields, then the only way you could pass data to an instance of the class is drop it somewhere for pickup.

Is there another way? Sure there is. Have it listen on a socket :). Or open a named pipe or memory-mapped file. Maybe just drop the data into a file that the instance looks for.

Of course, this is dumb.

It could also be done using reflection.
 

purbeast0

No Lifer
Sep 13, 2001
53,544
6,368
126
you are getting that error because type is not instantiated outside of the for loop, and you are sending the object to a function that will crap out on a null object.

if you simply were to do

Code:
card type = new card(1,1) ; //object for card class.

instead of how you have it, you won't get any error.

EDIT:

and the reason it can't take a null pointer in your case is because in System.out.println(Object) that you are calling, inside that function it calls the .toString() function on the Object passed in.

so if your variable type is null, and in there it tries to call type.toString(), it will throw an exception since type is null.
 
Last edited:

JC0133

Senior member
Nov 2, 2010
201
1
76
So if I do
card type = new card(1,1) ; //object for card class.
before the loop this should fix the issue.
 

purbeast0

No Lifer
Sep 13, 2001
53,544
6,368
126
yes.

where you have...

Code:
card type; //object for card class.

put this instead...

Code:
card type = new card(1,1) ; //object for card class.

and it will compile and run as expected.

but it is better that you understand WHY this is a problem rather than it just working heh. and all it is doing is GUARANTEEING, even if it totally skips that for loop, that when it hits the println function, the variable will be instantiated.

but obviously, the way you hardcoded the for loop and in your instance, that will never be the case. but the compiler just sees the for loop and not the for loop conditions.
 

JC0133

Senior member
Nov 2, 2010
201
1
76
I understand or I think I do. You are saying that if I assign it values inside the loop the compiler will not see it outside the loop when I call it in println right??

But it still concerns me that if I do this

for (newSuit=1; newSuit <= 4; newSuit++) {
for (newValue=1; newValue <= 13; newValue++) {
type=new card(newSuit,newValue);

System.out.println(type); //working
}
}

after I have done this in the beginning of the function.

card type = new card(1,1) ; //object for card class.

When I call this statement.
System.out.println(type);

Will it print out the values that I passed in with

type=new card(newSuit,newValue);

in the nested loop.

or outside the loop will it return back to

card type = new card(1,1) ;

?

Also since you have now declared new here with this statement

card type = new card(1,1) ;

am I still suppose to declare new or pass the values this way
in the nested loop

type=new card(newSuit,newValue);

?

These maybe dumb questions but you are right I need to understand
how and why it works vs just getting it to work.
 

JC0133

Senior member
Nov 2, 2010
201
1
76
For the first part I might not have explained it right. I am simply asking when I call

System.out.println(type);

outside the loop will it return the proper value.

I don't want it to return

card type = new card(1,1) ;
 

Glitchny

Diamond Member
Sep 4, 2002
5,679
1
0
For the first part I might not have explained it right. I am simply asking when I call

System.out.println(type);

outside the loop will it return the proper value.

I don't want it to return

card type = new card(1,1) ;

What do you want it to print exactly?
 
Last edited:

ioni

Senior member
Aug 3, 2009
619
11
81
For the first part I might not have explained it right. I am simply asking when I call

System.out.println(type);

outside the loop will it return the proper value.

I don't want it to return

card type = new card(1,1) ;

The print out after the loop should return the last object created inside the loop. What I don't get is how you ran the broken one in the 1st place. It doesn't even compile in Eclipse.
 

JC0133

Senior member
Nov 2, 2010
201
1
76
o no the first way I did it I got it to compile with

card type=new card(newSuit,newValue);

inside the loop but my professor told me not to do that. And I realized that

System.out.println(type); didn't work outside the loop and I didn't understand way. And when I did it my professors way, what I originally put on here, it still didn't work.

But I appreciate all your help. This should fix my issue.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
Code:
//this is inside of the main function
deckHand full; //object for full deck class.
deckHand empty; //object for empty deck class.
card type; //object for card class.[/QUOTE]

I'm just jumping in to nitpick, but if you need to comment every variable declaration then you need to rethink your naming.  Comments that state the obvious (or should be obvious from naming/type declared) are just as bad as not writing any comments at all.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Wait. Java really works that way?

The variable is declared outside the for loop. It's not going out of scope, and it's being assigned to an object in the loop. Why exactly is it losing the reference?
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
Wait. Java really works that way?

The variable is declared outside the for loop. It's not going out of scope, and it's being assigned to an object in the loop. Why exactly is it losing the reference?

Although this case is quite obvious, they aren't always.

Code:
int top = 0;
card type;

for (int i = 1; i < top; i++) {
  type = new card();
}

console.writeline(type);

or...

Code:
card type;

for(int i = 0; i < 10; i++)
{
  if (i == 0)
    break;
  type = new card();
}

console.writeline(type);

I'm far more familiar with C# than Java, but it does something similar for if statements.

Code:
card type;

if (sky == blue) {
  type = new card();
} else if (water == wet) {
  type = new card();
}

console.writeline(type);

This will not compile, use of unassigned local variable.

Code:
card type;

if (sky == blue) {
  type = new card();
} else {
  type = new card();
}

console.writeline(type);

This will compile.
 

hf2046

Junior Member
Sep 23, 2011
18
0
0
Code:
for (newSuit=1; newSuit <= 4; newSuit++) {
    for (newValue=1; newValue <= 13; newValue++) {
        type=new card(newSuit,newValue);

        System.out.println(type);  //working
    }
}
System.out.println(type);  //getting an error

I see something strange with your for loop declaration... shouldn't it be

Code:
for (int newSuit=1; newSuit <= 4; newSuit++) {
    for (int newValue = 1; newValue <= 13; newValue++) {
...
Unless newSuit and newValue are declared as ints outside of the loop, this code wouldn't even compile.
 
Last edited:

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81

Ok, I'm genuinely confused. If this is a compile time error, he shouldn't be able to label one statement as working and the other as not.

If it's a run time error, why isn't type remaining the new card it was assigned at the end of the loop?
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
Ok, I'm genuinely confused. If this is a compile time error, he shouldn't be able to label one statement as working and the other as not.

If it's a run time error, why isn't type remaining the new card it was assigned at the end of the loop?

I think JC was being vague. As ioni said, if you took the code posted in the OP and tried to compile it, it wouldn't work. I just took similar code and confirmed in VS2010 that it won't compile in C# either.
 

beginner99

Diamond Member
Jun 2, 2009
5,315
1,760
136
Obviously he didn't paste the whole code. classes are missing, and not all variables are declared. I'm also think he is getting a compilation error/warning and not a runtime error.

@OP:
Coding guidelines in java say that classes should always start with a Captial letter and variables with lower case. For me your code is already a bit confusing because of lower case class names.
 

hf2046

Junior Member
Sep 23, 2011
18
0
0
Obviously he didn't paste the whole code. classes are missing, and not all variables are declared. I'm also think he is getting a compilation error/warning and not a runtime error.

What would help a lot is if the OP included the stack trace when attempting to compile / execute the code...