« Return to Thread: no "Friend"s in Java

Re: no "Friend"s in Java

by Johnny Kewl :: Rate this Message:

Reply to Author | View in Thread

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 -----
From: matt.lengenfelder@...
To: nbusers@...
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....
 
    And then to remove the type dependency in the classes, read up on interfaces http://java.sun.com/docs/books/tutorial/
    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

 

From: Matthew Lengenfelder [mailto:matt.lengenfelder@...]
Sent: Wednesday, May 07, 2008 9:54 PM
To: nbusers@...
Subject: Re: [nbusers] 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.

 

From: Matthew Lengenfelder [mailto:matt.lengenfelder@...]
Sent: Wednesday, May 07, 2008 8:37 PM
To: nbusers@...
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

 


 « Return to Thread: no "Friend"s in Java