Solved

True & flase Statements

Posted on 2002-04-17
1
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 438
How to determine if a string is a valid SHA value 7 45
ejb mdb examples 1 21
Java: anonymous class 4 39
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 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