Win a copy of Securing DevOps this week in the Security forum!

J. Daniel

Greenhorn
+ Follow
since Nov 03, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by J. Daniel

Originally posted by Gina Pandher:
The output of the code is 10 and 40...i understand the result of 10 but how come 20 gets multiplied by 2 to give 40 and 10 is not multiplied..is it to do with post and pre increment or something else..
Someone please clarify
Thanx

public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int x){
x=x*2;
j=j*2;
}
}



In this case, x is passed as a parameter and j is a member variable. What that means is that a copy is made of x, so any changes to that copy are lost when the copy goes out of focus (at the end of amethod). However, the change to j is made directly to the member variable. Any changes to j are kept, since that variable stays in focus as long as this instance of the pass class exists (which, by the way, is true until the main method terminates in this case). Since I imagine this is all very confusing without more information on scope, you can check out the following tutorial, which I hope will help: http://www.jchq.net/tutorial/05_04Tut.htm
17 years ago

Originally posted by Rahder:
For example, two String objects may both have the value "Hi". They are considered "equal" even though there are two distinct objects.


For what it's worth...
This is only the case if both objects are declared as follows:
String str1 = "Hi";
String str2 = "Hi";
then str1 == str2 is true.
If instead the declarations were:
String str1 = "Hi";
String str2 = new String("Hi");
then str1 == str2 is false, unless you use the command:
str2 = str2.intern();
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned
17 years ago

Originally posted by Garrett:
Lets say I have a class that requires only the default constructor. I decided I wanted to block any other classes from making any calls to that constructor, so I hard coded the default constructor and made it private.
Then i thought, well if nobody can construct these objects, then their methods are useless... so I might as well make the whole class private. It said....
"modifier private not allowed here"
Assumptions
1.) To make the default constructor private, I must hard code
it and change it to private.
2.) Classes cannot be private? Ever?

Are these correct? I appreciate any input.



Just a couple of additional notes:
* Having a private constructor does not necessarily mean that no method in the class is unavailable. You can access static methods and variables without creating an instance of the class, assuming those items have appropriate permissions (e.g., they're public).
Here's an example (assume these classes are defined in separate source files):
//source file privConstructor.java
public class privConstructor
{
public static void amethod()
{
System.out.println("Ran amethod()");
}
private privConstructor(){}
}
// source file test.java
public class test
{
public static void main(String args[])
{
privConstructor.amethod();
}
}
* an inner (or nested class) can be private or protected. A top level class can only be declared public or default (no access modifier, also known as friendly, which means accessibly within that package). For more information, you can read http://www.jchq.net/tutorial/01_02Tut.htm
17 years ago

Originally posted by shinylee:
34. Assume that Sub1 and Sub2 are both subclasses of class Super.
Given the declarations:
Super super = new Super();
Sub1 sub1 = new Sub1();
Sub2 sub2 = new Sub2();
Which statement best describes the result of attempting to compile and execute the following statement:
super = sub1;
a) Compiles and definitely legal at runtime
b) Does not compile
c) Compiles and may be illegal at runtime
The correct answer is a), but I think b) is the right one.
super is a java keyword, and it cannot be used here.
Am I right? Thanks!



You are correct! See http://www.javaranch.com/ubb/Forum24/HTML/005321.html for more details.
17 years ago

Originally posted by shinylee:
53. A component that should resize vertically but not horizontally should be placed in a:
a) BorderLayout in the North or South location
b) FlowLayout as the first component
c) BorderLayout in the East or West location
d) BorderLayout in the Center location
e) GridLayout
The correct answer is c), but I think a) is the right one.
Am I right? Thanks!



Reading through this question, it states that the component should resize its height (vertically) but not its width (horizontally). An object in North/South would resize its width when a window is resized to be wider, but its height remains constant. An object in East or West allows its height to change, but not its width. Thus, c is the correct answer. Best of luck to you on the exam!
-J
17 years ago
Thanks for the excellent discussion on extends vs implements! It cleared up several items for me.
17 years ago