Solved

True & flase Statements

Posted on 2002-04-17
1
200 Views
Last Modified: 2010-03-31
hi..i need to know if the following sentences are true or false..and why? simple explanation plz

1) subclass variables should be defind as private to hide them from other subclasses. (        )
2) main method can't directly access any non static method and varibales,but using the instance of the class it can.!!!!!!


my comments or concerns..for the following :
1) i guess it should be true.. cuz of the encapsulation.. am i right..or it's not forced to be private .. only it's a better way to hide variables.. ?!?!!/ i'm not sure.just giving what i know..and waiting for ur answer

2) but why other static methods can't use instance of the class?!?!!?

hope i've made myself clear enough to get ur answers..
i'll be waiting..
0
Comment
Question by:ms_lost
1 Comment
 
LVL 86

Accepted Solution

by:
CEHJ earned 10 total points
ID: 6949262
1. True. As you say 'cuz of the encapsulation'. This is true generally of any class, not just subclasses. This prevents client classes from directly accessing the fields directly. Using accessor methods (get/set) provides a useful layer of indirection, such that the method may do things other than simple assignments (this.someProp = someProp).
2. True. 'but why other static methods can't use instance of the class?!?!!?' They can:

zclass StaticAccess {

  /**
   * Note that this variable must be declared 'static' to be accessible from
   * accessInstance() or any other static method
  */
  static StaticAccess instance;

  public static void accessInstance() {
    instance.printMessage();
  }
     
  public void printMessage(){
    System.out.println("In instance method");
  }

  public static void main(String[] args) {
    // Comment the next line out. What will the result be?
    instance = new StaticAccess();
    StaticAccess.accessInstance();
  }
}
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 56
Problem to start Neon 20 51
Groovy:unable to resolve class error 2 28
Java / Linux and Regular Expressions 11 45
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now