![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| ||||||||
| ||||||||
|
|
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? |
|
3. What means static? |
|
3.1. I know that I could use staic variables only from staitc mehtods. |
|
4.1. If I want to catch all Exceptions, I do this with "catch(Exception ex) {...}"? |
|
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... |
|
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. |
#4
| |||
| |||
|
|
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... |
#5
| ||||||
| ||||||
|
|
1.1. If I want to use my variables only inside of my class-file they must be private? They should be. That is not compulsory, but it makes sense to declare them private. |
|
1.2. Is this correct: If I want to use variables or methods from 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. |
|
1.3. What means protected? protected variables or methods can be accessed only from the same class or the same namespace (package in Java). |
|
3. What means static? 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. |
|
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) {...}" Yes, absolutely true. Also, you can get the error message by calling ex.getMessage(); |
#6
| |||
| |||
|
|
If the other class file is in the same namespace, then you can access only "public" or "protected" methods and variables. 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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |