Groups | Blog | Home
all groups > vj# > june 2005 >

vj# : Some understanding questions...


Christian-Josef Schrattenthaler
6/16/2005 11:01:22 AM
Hi!

I am still a beginner with Java/VJ#.NET/ASP.NET, and now I have some
understanding questions:

1. private, public and protected:
1.1. If I want to use my variables only inside of my class-file they
must be private?
1.2. Is this correct: If I want to use variables or methods from
outside the class-file thy must be public?
1.3. What means protected?

2. Sometimes I heard "If the page request is not a postback". What does
this mean?

3. What means static?
3.1. I know that I could use staic variables only from staitc mehtods.
But for what is "static"?

4. Exception handling with try/catch:
4.1. If I want to catch all Exceptions, I do this with "catch(Exception
ex) {...}"?
4.2. Else I could specify the exact Exception "catch(OleDbException)
{...}"?

Thanks for any help!!!

Greetings,
christian.
Christian-Josef Schrattenthaler
6/16/2005 11:17:11 AM
Hi!

I forgot:

5. "this.":
5.1. Why should I use "this.". In my Java-Learning-Book, and in manny
samples "this." is used, but I tried my code with and without it, and I
got every thime the same result -> NO ERROR...

Kind Greetings,
Christian.
Sushovan
6/17/2005 10:16:01 AM
Dear Christian-Josef Schrattenthaler,

[quoted text, click to view]
must be private?

They should be. That is not compulsory, but it makes sense to declare them
private.

[quoted text, click to view]
outside the class-file thy must be public?

If the other class file is in the same namespace, then you can access only
"public" or "protected" methods and variables.
Also, you can declare a variable private and write public accessor methods
to it, like

private int myPrivateNumber;
public int get_myPrivateNumber(){
return myPrivateNumber;
}
public void set_PrivateNumber(int value){
myPrivateNumber = value;
}

Using this method, you can verify whether the value assigned from outside is
valid or not before you actually assign it to a variable.


[quoted text, click to view]

protected variables or methods can be accessed only from the same class or
the same namespace (package in Java).

[quoted text, click to view]

This is large topic, but briefly, static means that the particular value
does not depend on what particular instance of the class you are
investigating. If you have one class, MyClass and 3 objects of type MyClass
named objOne, objTwo and objThree, then a static variable myStatVar will have
the same value for all of objOne, objTwo and objThree, and can be accessed by
calling MyClass.myStatVar.

[quoted text, click to view]

No! It's the other way round! Static methods can only access static
variables, but static variables can be accessed from non-static contexts as
well.


[quoted text, click to view]
ex) {...}"?
4.2. Else I could specify the exact Exception "catch(OleDbException)
{...}"

Yes, absolutely true. Also, you can get the error message by calling
ex.getMessage();

[quoted text, click to view]

"this" is especially useful when you are dealing with more than one classes.
Suppose you have two classes MyTopClass and MyBtmClass. MyTopClass creates an
object of type MyBtmClass and calls a method of that class. Suppose that
method needs to call a method of MyTopClass. This can be achieved by passing
the function the object "this", and then that function can use that object
reference to call functions in MyTopClass

This has been a long post... Hope it helps
--
Sushovan, a crazy junior


[quoted text, click to view]
George Birbilis [MVP J#] [9880]
6/18/2005 12:00:00 AM
[quoted text, click to view]

suppose you have the following code:

public class Test {

private int person;

public void tst(int person){
this.person=person;
}

}

you need "this" only in such scenarios, to differentiate class fields from
local variables (or function parameters) that have the same name (the local
scope has higher priority in name resolution, so a plain "person" would mean
the function parameter "person", not the class field "person"

of course, it's much better to use this pattern:

public class Test {

private int person;

public void tst(int thePerson){
person=thePerson;
}

}

in Borland-style you'd see "aPerson", but I prefer to write "thePerson"

cheers,
George

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com>
Microsoft Most Valuable Professional
MVP J# for 2004, 2005
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime (Delphi & ActiveX: VB, PowerPoint, .NET)
+ Plugs (InterProcess/Internet communication)
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................

George Birbilis [MVP J#] [9880]
6/18/2005 12:00:00 AM
[newsgroup microsoft.public.dotnet.vjsharp]
[quoted text, click to view]

if you don't add an access specifier they're package-private, that is any
class that has the same package name can access them (e.g. the
gr.cti.eslate.X class would be able to access such fields of gr.cti.eslate.Y
class)

[quoted text, click to view]

not sure if in J# you can access "protected" fields of a class from another
class that has same namespace or not. In Java you should be able to do so
only from class descendents, so this is problematic security-wise if this is
true (esp. when porting existing Java code to J#). Unless you can somehow
seal a namespace (as you can do with packages in Java), maybe by using a
signed assembly so that no other third-party code uses it apart from the
code you want. Since there's the notion of "package private" too as I
describe above, not sure why there should be separate namespace behaviour.
To be sure try it and see what it does

[quoted text, click to view]

also see the J# documentation on "properties". There are two get/set
patterns in J#, the JavaBeans-style (getXX/setXX) and the .NET style
(get_XX/set_XX), which differ a bit (the 2nd one I think is understood by J#
as accessors for .NET class properties and it exposes such automatically
[e.g. you could write myObject.xx=5 in C# or VB.net then instead of writing
myObject.setXX(5) if you wanted])

[quoted text, click to view]

see above my comment on "package private" and "protected". They should be
different things

in languages like Object Pascal, indeed all protected class members are
accessible by other classes inside the same unit, but that shouldn't be true
in Java

[quoted text, click to view]

there's also the notion of "static" function parameters, but they're rarely
used. See "Thinking in Java" free book by Bruce Eckel (www.eckelobjects.com)

[quoted text, click to view]

Unfortunately you can't catch .NET exceptions with "catch(Exception ex)" I
think. This is bad since that pattern is used a lot by Java programmers. Try
"catch(Throwable t)" instead, but that way you'll catch "Error" descendents
too (Exception and Error are descendents of Throwable class, and J# compiler
is understand "catch(Throwable t)" as catch-all [.NET exceptions included]
if I remember well, but not "catch(Exception e)" too)


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
George Birbilis <birbilis@kagi.com>
Microsoft Most Valuable Professional
MVP J# for 2004, 2005
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ QuickTime (Delphi & ActiveX: VB, PowerPoint, .NET)
+ Plugs (InterProcess/Internet communication)
http://www.kagi.com/birbilis
+ Robotics
http://www.mech.upatras.gr/~robgroup
.........................................................................

George Birbilis [MVP J#] [9880]
6/20/2005 10:18:27 PM
I've asked the J# team on this and will let you know about it

if anyone has time to checkout "Thinking in Java" book from
www.eckelobjects.com or the Java specification from java.sun.com on this,
please post your findings as a reply

[quoted text, click to view]

AddThis Social Bookmark Button