Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

sara adrem

Greenhorn
+ Follow
since Jul 26, 2007
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 sara adrem

>>>>> But what I gather is that the private modifier does not add anything special whether it is used in a static method or not, am I right?

It does make a difference. if you declare a method as private, that method can be called only within the code of that particular class

Eg...

A main method can also be declared final.

public static void main(String args[] { } is ok to use.
public static final void main(String args[] { } is also ok to use.



But a static method can never be overridden.
A parent class and child class can have a static method with the same signature and name. But they are not overridden methods.

A static method always hides any superclass static method with the same name.
C is correct.

For eg. object 1 (a,b) is (2,5)
object 2 is (2,5)
obj1 and obj2 are equal as per our definition

Hashcode a+b = 7 for both objects. so both objects are placed in same bucket.

if obj2 is (5,2), then also they are equal as per our definition.
here also hashcode is a+b= 7 for both objects and hence placed in same bucket.

For a-b this will not be true. Hence a-b is false
static void main(String[] args) {
}

Its a legal syntax for declaring a method called "main" with default access.
So there is no compilation problem.



If you want to run the "main" static method from command line, it needs to be declared public. Its defined by java spec.
Guys... Before you all mixup understand how static works.

static variables are class variables. They are initialized only once when the class is loaded into JVM. ie) before you create any instance/object of this class static variables are initialized.

static instance variables has default values. For objects it is null. So string will be initialized to null.

Come to example 1.
when class loads into JVM string title = null;

Now you call hellodemo(5) constructor for object d.

public hellodemo(int value)
{
super(); //inserted by compiler. call to constructor in Object class
this.value=value; // d.value = 5
//hellodemo();
}

Now you print static variable "null" is output
print d.value 5 is output


NOTE: static variable can be called either by classname or object.

hellodemo.title and d.title refers only to the same static variable title.
Static variables are class variables not instance variables.
[ March 14, 2008: Message edited by: sara adrem ]


o/p: i3 == i4

O/P is as expected.

Something wrong with your sample code
Its related to the use of Generics as well. You have restricted the value part of your map to Object class.

So map.get("..") returns an Object.

An Object can't be assigned to a String without explicit casting. Compilation should fail here.


String.equals(...) expects an Object as parameter. So it works without problems.
3 hrs / day. Its wonderful. You can make it easily. Have some worthy books like K&B. Also try simple programs and examples.
All methods declared in interfaces should be defined by Classes implementing interfaces.

If you want the Class not to implement all/some of the methods in interface, then the Class should be declared abstract. No other way.

Any class extending the abstract class will define those methods.
i= 0;

i = i++ is equivalent to the next 3 lines of code.

temp = 0;
i = i + 1; ie) i = 1 at this point of execution;
i = temp; ie) i = 0 again at this point of execution

So at the end of execution of i = i++, i has a value of 0.
A.class represents the java.lang.Class of Class A.

If aa is an object of A and if you want to get the Class to which aa is an object, use aa.getClass(). It returns the same java.lang.Class.

A.class and aa.getClass(), both Upon printing prints the class name in a readable manner along with the package name.


aa doesn't have an attribute called class. So aa.class won't compile.
Why do we get IllegalStateException??

>>MatchResult result = s.match();

match() method throws IllegalStateException if no match is available. To avoid this scenario use various next() methods of scanner to find match.
boolean and String can't be used as argument to switch.

//A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer (discussed in Simple Data Objects ).//

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html