• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Few tips

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It may help for the SCJP

* Interfaces may not be instantiated.
* You can have same identifier name for both instance variables and local(automatic) variables
* You can't have same identifier name for 2 classes or for nested classes.
* Instance variables are those which are declared in the class object
* Local variables are those which are declared inside the methods or functions.
* Runtime, Math, Color these classes can't be instantiated
* Constructors can be overloaded and overridden
* Constructors doesn't have return type
* Constructors can be declared private.
* Instance variables are initialized by default value of 0 for numberic data types and null for string and character data types , if not initialized explicitly.
* Local variables are not initialized by default values , if not explictly initialized.
* Use of static modifier is - it can be used independent of object name and it retains its value through out the program between the calls .
* String class modifier methods are concat(),replace(), substring() and trim().
* String class is immutable (means we can't modify the values)
* String and StringBuffer classes can't be compared with equlas() method, but String and String values can be checked for equality and also StringBuffer and StringBuffer values can be checked for equality.
*(java ranch round rules question) class y is a subclass of x ,
Y myY = new Y();
X myX = myY;
This will work because the child/subclass object is guaranteed to have the methods of the superclass.
* (java ranch round rules question)It is legal to access a static method using an instance of the class.
* (java ranch round rules question)It is not possible to hava an instance of a non-static inner class before any instances of the outerclass have been created.
* Forward referencing means using the variable before initializing it.

 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sdev,
* Instance variables are those which are declared in the class object
//Not quite accurate, even local variables are declared in the class object.
* Local variables are those which are declared inside the methods or functions.
//Even just in statement blocks,enclosed between braces.
* Runtime, Math, Color these classes can't be instantiated
//My doubts on Color, I suppose this is legal
Color myColor = new Color(255,0,0)
* Constructors can be overloaded and overridden
//Constructor Overriding, No! No!No!.We are able to override methods because we inherit them from our base classes.ButCONSTRUCTORS ARE NEVER INHERITED, so the issue of overriding does not arise.
* Instance variables are initialized by default value of 0 for numberic data types and null for string and character data types , if not initialized explicitly.
//What are Character data types ?. Better said as .....and null for reference[/b types.
* Use of static modifier is - it can be used independent of object name and it
retains its value through out the program between the calls .
[b]//Even a non-static member variable
retains value throughout the program between calls.
* String class modifier methods are concat(),replace(), substring() and trim().
//They sound like modifier methods, but they are not.Or it depends upon your definition of modifier methods.Recall the immutability of String objects, so they dont get modified.
* String class is immutable (means we can't modify the values)
//exactly, so you see what I mean above.

* Forward referencing means using the variable before initializing it.
//It means using a variable before declaring it.Forward referencing is legal in certain circumstances, like if it is not being used in a variable initializer.
Herbert
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank You very much for correcting all of my errors
Can you please explain differeces between instance and local variables and where can we declare both.
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sdev,
Consider the sample class below, to see the different levels at which you can declare the different variables.
<center>
<h4>Levels of Variable Declaration </h4>


<code>

public class VariableSamples

{

 int intanceVar;

  //An instance variable....each instance will have its own copy

 static int classVar;

  // A Static variable...the same value available to all instances
 public void SampleMethod(){

   short localVar1 =0;

   // A local variable...needs explicit initialisation

 }
 {

  byte anotherLocalVariable =0; //A local variable inside a block

 }

}
</code>
If you have further questions, Welcome
Herbert
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, the above post has not been rendered the way I intended it to.Let me try again


<code>

public class VariableSamples

{

 int intanceVar;

  //An instance variable....each instance will have its own copy

 static int classVar;

  // A Static variable...the same value available to all instances
 public void SampleMethod(){

   short localVar1 =0;

   // A local variable...needs explicit initialisation

 }
 {

  byte anotherLocalVariable =0; //A local variable inside a block

 }

}
</code>

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sdev,
I am not agree on one point. u said :


String and StringBuffer classes can't be compared with equlas() method, but String and String values can be checked for equality and also StringBuffer and StringBuffer values can be checked for equality.


I would say u can use equals() method for StringBuffer and StringBuffer values but it will always return false, b'cuz StringBuffer class does not override equals() method. i have written a simple program to show my point.

public class Test {
public static void main(String args[]) {

StringBuffer a = new StringBuffer("viv");
StringBuffer b = new StringBuffer("viv");
System.out.println(a.equals(b));//print false
System.out.println(a == b);// print false
}
}

i hope u would be agree on my point.
vivek

[This message has been edited by Vivek Shrivastava (edited July 11, 2000).]
[This message has been edited by Vivek Shrivastava (edited July 11, 2000).]
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi, Herbert Maosa
bartender
Thank u very much I got an idea of variables now . Below I have a question and answer . Could u explain it to me why the answer is B. I couldn't get it, I tried executing this program , I didn't get any complilation or runtime errors.


What happens when the following code executes?
1:class Base{
2: // legal code
3:}
4:class Derived1 extends Base{
5: // legal code
6:}
7:class Derived2 extends Base{
8: // legal code
9:}
10 ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
19: }
20:}
A. Compile time error at line 14
B. Run time error at line 18
C. Compile time error at line 17
D. NO ERRORS
Answer :
B. Run time error at line 18
b is of type Base and points to d1 and d2 is neither so at run
time it fails


 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SDev,
Here are a few things that I would like you to revisit.
Constructors can be overloaded and overridden
This is not entirely correct. While constructors can be overloaded, they cannot be overridden because constructors are not inherited.
Local variables are not initialized by default values , if not explictly initialized.
One exception to this rule is arrays. Array variables are always initialized whether declared as an instance variable or a member variable.
String class is immutable (means we can't modify the values)
From a purist point of view, it should read String objects are immutable.
Hope I am not being too picky
Ajith
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi, Herbert Maosa
bartender

I am sorry in my last reply, that I have sent with the question and answer , it has taken that : p smiles legend in place of ublic
Sahana
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u vivek , I was wrong
 
His brain is the size of a cherry pit! About the size of this ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic