
|
no "Friend"s in Java
I'm started to do some java programming in netbeans and thought I'd toss this out there.
simple example: create a new project - Java Desktop Application, basic application "D1"
on "D1View" drop some controls in the design view, create a new java class create a function in that class and try to connect to any of the controls you created in the D1View class
for instance you can't set the value of JTextArea1
I've tried making a new function for instance
public void setText(String test) { jTextArea1.append(test); }
in
the D1View class that takes a string and appends
JTextArea1 and it works but that function also can't be called outside
of the class...
There is no way to set "Friend" classes in Java.
public
class D1View - this is how the class is declared, so
it's public and my simple setText function is public, why in the world
will it not let me access it in another class, in the same package?
if I set "setText" to be static then other classes can see it, but it won't compile while accepting a string.
I've
also gone through the designer and used custom code to set both JTextArea1 and the
mainPanel to be public in the hopes that something would show up as
usable in the NewClass but so far nothing is.
Thoughts?
Matt Lengenfelder
|

|
RE: no "Friend"s in Java

Some parts of this message have been removed.
Learn more about Nabble's security policy.
What do you mean “can’t be called”? It can and
will.
How do you call it?
If write following two lines:
D1View myView = new D1View();
myView.setText(“XYZ”);
It will do what you want.
Friend modifier in C++ is an abomination.
I'm started to do some java programming in netbeans and
thought I'd toss this out there.
simple example:
create a new project - Java Desktop Application, basic application
"D1"
on "D1View" drop some controls in the design view,
create a new java class
create a function in that class and try to connect to any of the controls you
created in the D1View class
for instance you can't set the value of JTextArea1
I've tried making a new function for instance
public void setText(String test) { jTextArea1.append(test);
}
in the D1View class that takes a string and appends JTextArea1 and it works but
that function also can't be called outside of the class...
There is no way to set "Friend" classes in Java.
public class D1View
- this is how the class is declared, so it's public and my simple setText
function is public, why in the world will it not let me access it in another
class, in the same package?
if I set "setText" to be static then other classes can see it, but it
won't compile while accepting a string.
I've also gone through the designer and used custom code to set both JTextArea1
and the mainPanel to be public in the hopes that something would show up as
usable in the NewClass but so far nothing is.
Thoughts?
Matt Lengenfelder
|

|
Re: no "Friend"s in Java
My NewClass file: -------------------------- package d1; public class NewClass { public void test(){ D1View myView = new D1View(); myView.jTextArea1.append("XYZ");
} } The error I get running the application: ----------------------------- init: deps-jar: Compiling 1 source file to C:\programs\java\D1\build\classes C:\programs\java\D1\src\d1\NewClass.java:5: cannot find symbol
symbol : constructor D1View() location: class d1.D1View D1View myView = new D1View(); 1 error BUILD FAILED (total time: 0 seconds) I guess I just havn't used Gui builders enough, wouldn't that create a new object/form? I don't want a new form I want to be able to change the existing one.
On Wed, May 7, 2008 at 11:16 PM, Gary Greenberg < gary.greenberg@...> wrote:
What do you mean "can't be called"? It can and
will.
How do you call it?
If write following two lines:
D1View myView = new D1View();
myView.setText("XYZ");
It will do what you want.
Friend modifier in C++ is an abomination.
I'm started to do some java programming in netbeans and
thought I'd toss this out there.
simple example:
create a new project - Java Desktop Application, basic application
"D1"
on "D1View" drop some controls in the design view,
create a new java class
create a function in that class and try to connect to any of the controls you
created in the D1View class
for instance you can't set the value of JTextArea1
I've tried making a new function for instance
public void setText(String test) { jTextArea1.append(test);
}
in the D1View class that takes a string and appends JTextArea1 and it works but
that function also can't be called outside of the class...
There is no way to set "Friend" classes in Java.
public class D1View
- this is how the class is declared, so it's public and my simple setText
function is public, why in the world will it not let me access it in another
class, in the same package?
if I set "setText" to be static then other classes can see it, but it
won't compile while accepting a string.
I've also gone through the designer and used custom code to set both JTextArea1
and the mainPanel to be public in the hopes that something would show up as
usable in the NewClass but so far nothing is.
Thoughts?
Matt Lengenfelder
|

|
Re: no "Friend"s in Java
One way to do it, that I use often, is to create a null object of the
D1View in the class you need to be able to change the JTextArea1 text from:
public class MyClass {
private D1View frmD1View;
Then, in the MyClass constructor, accept a parameter of type D1View and
create an instance of MyClass in your D1View object.
public class D1View extends JFrame {
private MyClass myClass;
public D1View() {
initComponents();
myClass = new MyClass(this);
...
} // End of D1View constructor.
} // End of D1View frame.
public class MyClass {
private D1View frmD1View;
public MyClass ( D1View view) {
this.frmD1View = view;
} // end of MyClass constructor.
private void someFunction () {
this.frmD1View.append ( "XYZ" );
} // end of someFunction().
} // End of MyClass.
This basically creates a "pointer" to the current instance of D1View
that is in operation, since your MyClass constructor was called with
"this" from the D1View constructor.
--
Cheers,
Sean Carrick
PekinSOFT Systems
sean@...
http://www.pekinsoft.net----------------------------------
Proud to be 100% Microsoft free...
----------------------------------
|

|
Re: no "Friend"s in Java

Some parts of this message have been removed.
Learn more about Nabble's security policy.
--------------------------------------------------------------------------- HARBOR:
http://coolharbor.100free.com/index.htmThe
most powerful application server on earth. The only real POJO Application
Server. Making the Java dream come
true. ---------------------------------------------------------------------------
Matthew,
You just on a little learning curve, and you coming
from C++ and it is a little weird at first.
I remember struggling with trying to see what java
did with pointers.
Its still all possible, its a little different but
much much easier once you click.
C++ programmers have great insight, and you'll end
up making superb apps.. just hang in there a little.
Two things to get you going really
quick...
Google for the SUN JAVA TUT... read the whole
thing
In the SWING... UI stuff... if in doubt right click
on a UI component and click javadoc... that will have a reference to another tut
with enuf examples to make you click.
Lots to learn... but it wont be long before you
show us a few new tricks ;)
Friends... no but living without them, is no sweat
in Java... I dont really miss pointers either ;)
Typedef... not something I miss too much either
;)
Its different... but Java is very very clean... it
is a beautiful simple language.
To me Java is (over simplified), something that
happens if a programmer has used C++ for a million years.
Once they get all their tools working, and automate
memory management and its perfect... then its Java ;)
Have fun ;)
----- Original Message -----
Sent: Thursday, May 08, 2008 5:37
AM
Subject: [nbusers] no "Friend"s in
Java
I'm started to do some java programming in netbeans and thought
I'd toss this out there.
simple example: create a new project - Java
Desktop Application, basic application "D1" on "D1View" drop some controls
in the design view, create a new java class create a function in that
class and try to connect to any of the controls you created in the D1View
class for instance you can't set the value of JTextArea1
I've tried
making a new function for instance
public void
setText(String test) { jTextArea1.append(test); }
in the D1View class
that takes a string and appends JTextArea1 and it works but that function also
can't be called outside of the class...
There is no way to set "Friend"
classes in Java.
public class D1View - this is how the class
is declared, so it's public and my simple setText function is public, why in
the world will it not let me access it in another class, in the same
package?
if I set "setText" to be static then other classes can see it,
but it won't compile while accepting a string.
I've also gone through
the designer and used custom code to set both JTextArea1 and the mainPanel to
be public in the hopes that something would show up as usable in the NewClass
but so far nothing is.
Thoughts?
Matt Lengenfelder
|

|
Re: no "Friend"s in Java
tyvm :) Matt Lengenfelder On Thu, May 8, 2008 at 1:34 AM, Sean Carrick < seansitsolutions@...> wrote:
One way to do it, that I use often, is to create a null object of the D1View in the class you need to be able to change the JTextArea1 text from:
public class MyClass {
private D1View frmD1View;
Then, in the MyClass constructor, accept a parameter of type D1View and create an instance of MyClass in your D1View object.
public class D1View extends JFrame {
private MyClass myClass;
public D1View() {
initComponents();
myClass = new MyClass(this);
...
} // End of D1View constructor.
} // End of D1View frame.
public class MyClass {
private D1View frmD1View;
public MyClass ( D1View view) {
this.frmD1View = view;
} // end of MyClass constructor.
private void someFunction () {
this.frmD1View.append ( "XYZ" );
} // end of someFunction().
} // End of MyClass.
This basically creates a "pointer" to the current instance of D1View that is in operation, since your MyClass constructor was called with "this" from the D1View constructor.
--
Cheers,
Sean Carrick
PekinSOFT Systems
sean@...
http://www.pekinsoft.net
----------------------------------
Proud to be 100% Microsoft free...
----------------------------------
|

|
Re: no "Friend"s in Java
Java & has a more fine grained approach to visibility scoping than the current one. My understanding is that it will let you do something like declare classes to be friends. On Thu, May 8, 2008 at 7:59 AM, Matthew Lengenfelder < matt.lengenfelder@...> wrote:
tyvm :)
Matt Lengenfelder
On Thu, May 8, 2008 at 1:34 AM, Sean Carrick < seansitsolutions@...> wrote:
One way to do it, that I use often, is to create a null object of the D1View in the class you need to be able to change the JTextArea1 text from:
public class MyClass {
private D1View frmD1View;
Then, in the MyClass constructor, accept a parameter of type D1View and create an instance of MyClass in your D1View object.
public class D1View extends JFrame {
private MyClass myClass;
public D1View() {
initComponents();
myClass = new MyClass(this);
...
} // End of D1View constructor.
} // End of D1View frame.
public class MyClass {
private D1View frmD1View;
public MyClass ( D1View view) {
this.frmD1View = view;
} // end of MyClass constructor.
private void someFunction () {
this.frmD1View.append ( "XYZ" );
} // end of someFunction().
} // End of MyClass.
This basically creates a "pointer" to the current instance of D1View that is in operation, since your MyClass constructor was called with "this" from the D1View constructor.
--
Cheers,
Sean Carrick
PekinSOFT Systems
sean@...
http://www.pekinsoft.net
----------------------------------
Proud to be 100% Microsoft free...
----------------------------------
|

|
RE: no "Friend"s in Java

Some parts of this message have been removed.
Learn more about Nabble's security policy.
you do need a default constructor in your D1View class.
I presumed you had one.
My NewClass file:
--------------------------
package d1;
public class NewClass {
public void test(){
D1View myView = new D1View();
myView.jTextArea1.append("XYZ");
}
}
The error I get running the application:
-----------------------------
init:
deps-jar:
Compiling 1 source file to C:\programs\java\D1\build\classes
C:\programs\java\D1\src\d1\NewClass.java:5: cannot find symbol
symbol : constructor D1View()
location: class d1.D1View
D1View myView = new D1View();
1 error
BUILD FAILED (total time: 0 seconds)
I guess I just havn't used Gui builders enough, wouldn't that create a new
object/form? I don't want a new form I want to be able to change the existing
one.
On Wed, May 7, 2008 at 11:16 PM, Gary Greenberg <gary.greenberg@...>
wrote:
What do you mean "can't be
called"? It can and will.
How do you call it?
If write following two lines:
D1View myView = new D1View();
myView.setText("XYZ");
It will do what you want.
Friend modifier in C++ is an
abomination.
I'm started to do some java programming in netbeans and thought I'd toss
this out there.
simple example:
create a new project - Java Desktop Application, basic application
"D1"
on "D1View" drop some controls in the design view,
create a new java class
create a function in that class and try to connect to any of the controls you
created in the D1View class
for instance you can't set the value of JTextArea1
I've tried making a new function for instance
public void setText(String test) { jTextArea1.append(test);
}
in the D1View class that takes a string and appends JTextArea1 and it works but
that function also can't be called outside of the class...
There is no way to set "Friend" classes in Java.
public class D1View
- this is how the class is declared, so it's public and my simple setText
function is public, why in the world will it not let me access it in another
class, in the same package?
if I set "setText" to be static then other classes can see it, but it
won't compile while accepting a string.
I've also gone through the designer and used custom code to set both JTextArea1
and the mainPanel to be public in the hopes that something would show up as
usable in the NewClass but so far nothing is.
Thoughts?
Matt Lengenfelder
|

|
Re: no "Friend"s in Java
On Thu, May 8, 2008 at 12:57 PM, Gary Greenberg < gary.greenberg@...> wrote:
you do need a default constructor in your D1View class.
I presumed you had one. I'm just using the default
program template(File, new project, java desktop application) with one added control (JTextArea1) and one other class to access
the control. If the people who wrote netbeans (and the template) didn't build a default constructor into the template... well I don't feel the need to add one. besides I only want the one form and I should be able to assume their template creates a single form properly.
My problem was that I saw no way of accessing the control on that form without being in the class (and have read that java doesn't have friend classes that would allow this), even if I created a public function, the public function can't be accessed either because of how scope works in java. well perhaps scope is the wrong word. What I should be saying is that I didn't understand the difference between instance variables and class variables; or static vs non-static fields.
# Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
# Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
Setting the control on the form to static allows it to be accessed from other classes as I would expect. Since I only want one instance of this form running, ever, I don't really see a problem with setting it to static. Other than the rare chance that 2 methods try to write to the JTextArea at the exact same time. I'd like to think that JVM would deal with this, but perhaps I can't assume that.
If I created a new form which is what you were suggesting with your code, how would I then be able to access a control on that form from a 3rd class? I'd assume this would result in a similar problem and hence the best solution appears to be just setting it to static and spend some time figuring out/reading about how java deals with multiple methods in different classes accessing the same control at the same time (does this break the code or does it handle it elegantly?)
and If I shouldn't set the control to be static. Then I'd take another look at the code Sean kindly suggested and see if that will work for me.
Matt Lengenfelder
My NewClass file:
--------------------------
package d1;
public class NewClass {
public void test(){
D1View myView = new D1View();
myView.jTextArea1.append("XYZ");
}
}
The error I get running the application:
-----------------------------
init:
deps-jar:
Compiling 1 source file to C:\programs\java\D1\build\classes
C:\programs\java\D1\src\d1\NewClass.java:5: cannot find symbol
symbol : constructor D1View()
location: class d1.D1View
D1View myView = new D1View();
1 error
BUILD FAILED (total time: 0 seconds)
I guess I just havn't used Gui builders enough, wouldn't that create a new
object/form? I don't want a new form I want to be able to change the existing
one.
On Wed, May 7, 2008 at 11:16 PM, Gary Greenberg <gary.greenberg@...>
wrote:
What do you mean "can't be
called"? It can and will.
How do you call it?
If write following two lines:
D1View myView = new D1View();
myView.setText("XYZ");
It will do what you want.
Friend modifier in C++ is an
abomination.
I'm started to do some java programming in netbeans and thought I'd toss
this out there.
simple example:
create a new project - Java Desktop Application, basic application
"D1"
on "D1View" drop some controls in the design view,
create a new java class
create a function in that class and try to connect to any of the controls you
created in the D1View class
for instance you can't set the value of JTextArea1
I've tried making a new function for instance
public void setText(String test) { jTextArea1.append(test);
}
in the D1View class that takes a string and appends JTextArea1 and it works but
that function also can't be called outside of the class...
There is no way to set "Friend" classes in Java.
public class D1View
- this is how the class is declared, so it's public and my simple setText
function is public, why in the world will it not let me access it in another
class, in the same package?
if I set "setText" to be static then other classes can see it, but it
won't compile while accepting a string.
I've also gone through the designer and used custom code to set both JTextArea1
and the mainPanel to be public in the hopes that something would show up as
usable in the NewClass but so far nothing is.
Thoughts?
Matt Lengenfelder
|

|
Re: no "Friend"s in Java
That was supposed to say Java 7 not Java & LOL... On Thu, May 8, 2008 at 11:25 AM, software visualization < softwarevisualization@...> wrote:
Java & has a more fine grained approach to visibility scoping than the current one. My understanding is that it will let you do something like declare classes to be friends.
On Thu, May 8, 2008 at 7:59 AM, Matthew Lengenfelder < matt.lengenfelder@...> wrote:
tyvm :)
Matt Lengenfelder
On Thu, May 8, 2008 at 1:34 AM, Sean Carrick < seansitsolutions@...> wrote:
One way to do it, that I use often, is to create a null object of the D1View in the class you need to be able to change the JTextArea1 text from:
public class MyClass {
private D1View frmD1View;
Then, in the MyClass constructor, accept a parameter of type D1View and create an instance of MyClass in your D1View object.
public class D1View extends JFrame {
private MyClass myClass;
public D1View() {
initComponents();
myClass = new MyClass(this);
...
} // End of D1View constructor.
} // End of D1View frame.
public class MyClass {
private D1View frmD1View;
public MyClass ( D1View view) {
this.frmD1View = view;
} // end of MyClass constructor.
private void someFunction () {
this.frmD1View.append ( "XYZ" );
} // end of someFunction().
} // End of MyClass.
This basically creates a "pointer" to the current instance of D1View that is in operation, since your MyClass constructor was called with "this" from the D1View constructor.
--
Cheers,
Sean Carrick
PekinSOFT Systems
sean@...
http://www.pekinsoft.net
----------------------------------
Proud to be 100% Microsoft free...
----------------------------------
|

|
Re: no "Friend"s in Java

Some parts of this message have been removed.
Learn more about Nabble's security policy.
--------------------------------------------------------------------------- HARBOR:
http://coolharbor.100free.com/index.htm The
most powerful application server on earth. The only real POJO Application
Server. Making the Java dream come
true. ---------------------------------------------------------------------------
----- Original Message -----
Sent: Friday, May 09, 2008 1:25 AM
Subject: Re: [nbusers] no "Friend"s in
Java
On Thu, May 8, 2008 at 12:57 PM, Gary Greenberg < gary.greenberg@...>
wrote:
you do need a
default constructor in your D1View class.
I presumed you had
one.
I'm just using the default program template(File, new project, java
desktop application) with one added control (JTextArea1) and one other class
to access the control. If the people who wrote netbeans (and the template)
didn't build a default constructor into the template... well I don't feel the
need to add one. besides I only want the one form and I should be able to
assume their template creates a single form properly.
My problem was
that I saw no way of accessing the control on that form without being in the
class (and have read that java doesn't have friend classes that would allow
this), even if I created a public function, the public function can't be
accessed either because of how scope works in java. well perhaps scope is the
wrong word. What I should be saying is that I didn't understand the difference
between instance variables and class variables; or static vs non-static
fields.
# Instance Variables (Non-Static Fields) Technically
speaking, objects store their individual states in "non-static fields", that
is, fields declared without the static keyword. Non-static fields are also
known as instance variables because their values are unique to each instance
of a class (to each object, in other words); the currentSpeed of one bicycle
is independent from the currentSpeed of another.
# Class Variables
(Static Fields) A class variable is any field declared with the static
modifier; this tells the compiler that there is exactly one copy of this
variable in existence, regardless of how many times the class has been
instantiated. A field defining the number of gears for a particular kind of
bicycle could be marked as static since conceptually the same number of gears
will apply to all instances. The code static int numGears = 6; would create
such a static field. Additionally, the keyword final could be added to
indicate that the number of gears will never change.
Setting the
control on the form to static allows it to be accessed from other classes as I
would expect. Since I only want one instance of this form running, ever, I
don't really see a problem with setting it to static. Other than the rare
chance that 2 methods try to write to the JTextArea at the exact same time.
I'd like to think that JVM would deal with this, but perhaps I can't assume
that.
No you cant assume SWING is thread safe... but that doesnt have much to
do with whether it should be static or not.
Just whether you are going to
have multiple threads, and thats also not the same as just having 2 classes
referencing the same thing.
You'd use static if say you
wanted to count how many instances of the class are in use... something like
that.
But not to just make accessing
a function convenient, thats probably trouble if a user does open two
forms.
If I created a new form which is what you were suggesting with your
code, how would I then be able to access a control on that form from a 3rd
class? I'd assume this would result in a similar problem and hence the best
solution appears to be just setting it to static and spend some time figuring
out/reading about how java deals with multiple methods in different classes
accessing the same control at the same time (does this break the code or does
it handle it elegantly?)
If I remember in C++ it was
called a callback, thats what they showing you...
But I think you
thinking...
In your main function...
something like
ClassUI classUI = new
classUI();
ClassMyThing classMyThing =
new ClassMyThing(classUI);
ClassYourThing classYourThing
= new ClassYourThing(classUI);
//As many as you
want....
Matt you've just lost your
pointers and its confused the hell out of you (and us)... read the tut... by
the time you get thru it, you'll get it.
and If I shouldn't set the control to
be static. Then I'd take another look at the code Sean kindly suggested and
see if that will work for me.
I dont think thats the right
reason for static off hand... rather just pass references... in Java thats
just the class... Java is figuring out underneath whether its a pointer or a
class... where have all those damn pointers gone? ;)
Go through the tut... because
we now more confused than you are... ha ha
If the next question is about
destructors... we are going to have to kill you ;)
Matt
Lengenfelder
My NewClass
file: -------------------------- package d1;
public class
NewClass { public void
test(){ D1View myView = new
D1View();
myView.jTextArea1.append("XYZ"); } }
The
error I get running the
application: ----------------------------- init: deps-jar: Compiling
1 source file to
C:\programs\java\D1\build\classes C:\programs\java\D1\src\d1\NewClass.java:5:
cannot find symbol symbol : constructor D1View() location: class
d1.D1View D1View myView = new
D1View(); 1
error BUILD FAILED (total time: 0 seconds)
I guess I just havn't
used Gui builders enough, wouldn't that create a new object/form? I don't
want a new form I want to be able to change the existing
one.
On Wed, May 7, 2008 at 11:16 PM, Gary Greenberg <gary.greenberg@...> wrote:
What do you mean
"can't be called"? It can and will.
How do you call
it?
If write following
two lines:
D1View myView
= new D1View();
myView.setText("XYZ");
It will do what you
want.
Friend modifier in
C++ is an abomination.
I'm started to do some java programming in netbeans and thought I'd toss
this out there.
simple example: create a new project - Java
Desktop Application, basic application "D1" on "D1View" drop some
controls in the design view, create a new java class create a
function in that class and try to connect to any of the controls you created
in the D1View class for instance you can't set the value of
JTextArea1
I've tried making a new function for
instance
public void setText(String test) {
jTextArea1.append(test); }
in the D1View class that takes a string
and appends JTextArea1 and it works but that function also can't be called
outside of the class...
There is no way to set "Friend" classes in
Java.
public class D1View - this is how the class is
declared, so it's public and my simple setText function is public, why in
the world will it not let me access it in another class, in the same
package?
if I set "setText" to be static then other classes can see
it, but it won't compile while accepting a string.
I've also gone
through the designer and used custom code to set both JTextArea1 and the
mainPanel to be public in the hopes that something would show up as usable
in the NewClass but so far nothing is.
Thoughts?
Matt
Lengenfelder
|