Java Questions

JC0724

Weaksauce
Joined
Oct 11, 2008
Messages
105
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??
 
yeah you usually leave the default constructor alone and instead override the constructor. take for example:

say you have a class called test,

default constructor would be something along the lines of:

public test() {
}

you then override it with say:

public test(param1, param2) {
param1 = 0;
param2 = 0;
}

when no arguments are pass the default constructor test() is call instead of the constructor with arguments.
 
yeah you usually leave the default constructor alone and instead override the constructor.
But that's what he was told not to do...

Is there another way to pass information to a class from another class besides a Method or a Constructor?
Not really.

I'm guessing this restriction only applies to your top-level class. You should be free to write the rest of them however you want. You simply can't create a reasonable OO design when no class has an externally accessible interface.
 
it's probably just sidestepping your professor's intent but you can create a method that's "package private" by declaring it as "void myMethod(type arg1) { ... }", do not put public or private in the declaration.

if everything's in the same package (or the default package as is usually the case for programming class type assignments) it will work.
 
Is this an assignment where he is trying to teach you something or did he say its bad practice to do that? I am confused as to why you wouldn't override a default constructor or have private methods as "bad practice".

If its just an assignment, then whatever, I curious as to the answer then.
 
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.

//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
 
Imho there are at least following ways:
1. Public Members - make some members on a class public and change them from another class to transfer information. Bad practise but solves your problem.

2. File - make a shared file and put information inside it. Both classes should know about this file to share information between each other.
 
O and to add details to the error.

The error is "type cannot be resolved to a variable"
 
i suspect it is more likely that "the local variable type may not have been initialized". just declare "card type = null;". the compiler isn't smart enough to know that type will have a value after the for loop.
 
what do you mean scope?

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

System.out.println(type); //working can see type because it's in this loop
}
}
System.out.println(type); //getting an error Can't see type because it's outside of the closing {}.

*edit, pretty much the same thing up there. If you declare type = null beforehand, it will be pointing toward an empty spot in memory. But... it will be in scope so your compile won't fail.
 
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 = null"

When I call this statement outside the loop
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 return back to the null value I assigned in the beginning?

Also is this the prepare way to assign it?

"card type = null"

shouldn't it be

"card type = new null" or "type = new card null" or something like that?

I am just so used to using new creating an instance or storing space for an object.
 
It should just point to null.

er... pass the variables you want from inside the loop to a method, have it return it, then

System.out.println(typeMethod());


Maybe. I'm about 1/4 stoned on lortabs and flexerils for a bad back. I could very well be telling you how to get transparent aluminum or something.
 
Roscowgo i'm afraid you are incorrect.

a variable will retain it's value outside of a loop, which is to say type's value will be whatever was set during the last iteration of the loop, i.e. "new card(4,13)" in this case.

the error is because the compiler detects the possibility of not assigning a value to a variable since it is possible for a for loop to run with 0 iterations. in this specific case you know for certain it will always assign a value but the compiler doesn't.

JC0724 the proper way to set something to null is exactly as i wrote it. null denotes nothingness, you are not creating a new instance of anything.
 
Roscowgo i'm afraid you are incorrect.

a variable will retain it's value outside of a loop, which is to say type's value will be whatever was set during the last iteration of the loop, i.e. "new card(4,13)" in this case.

the error is because the compiler detects the possibility of not assigning a value to a variable since it is possible for a for loop to run with 0 iterations. in this specific case you know for certain it will always assign a value but the compiler doesn't.

JC0724 the proper way to set something to null is exactly as i wrote it. null denotes nothingness, you are not creating a new instance of anything.

I'm good at being wrong. I do it all the time. :D I haven't looked at java in at least a year. And didn't do much with it then.

thanks!
 
Back
Top